-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-functions-wip.php
71 lines (60 loc) · 1.38 KB
/
11-functions-wip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>
<head>
<title>functions a foot</title>
</head>
<body>
<p>Code to say hello to Fred by default but recieve dynamic variable</p>
<?php
function sayHello($name = 'Fred')
{
return sprintf("Hello %s", $name );
}
echo sayHello();
?>
<hr />
<p>function to apply pre tags around print_r</p>
<?php
$idxfamily = array('nev', 'clare', 'freddy');
function pp($arr)
{
echo "<pre>";
print_r($arr);
echo "</pre>";
}
pp($idxfamily);
?>
<hr />
<p>Array pluck </p>
<?php
$people = array(
array('name' => 'nev', 'age' => '41', 'job' => 'web dev'),
array('name' => 'clare', 'age' => '36', 'job' => 'events'),
array('name' => 'fred', 'age' => '0', 'job' => 'being cute'),
);
function arrPluck($arr, $topluck = 'name')
{
$out = array();
foreach($arr as $item)
{
array_push($out, $item[$topluck]);
}
return $out;
}
var_dump(arrPluck($people, 'name'));
?>
<hr />
<p>Array pluck using array map</p>
<?php
function arrPluckMap($arr, $topluck = 'name')
{
return array_map(function($item) use ($topluck)
{
return $item[$topluck];
}
,$arr);
}
var_dump(arrPluckMap($people, 'job'));
?>
</body>
</html>