-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path028-arrays-reset-end-array_filter-array_map.php
More file actions
40 lines (35 loc) · 1.17 KB
/
028-arrays-reset-end-array_filter-array_map.php
File metadata and controls
40 lines (35 loc) · 1.17 KB
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
<?php
function arrayOutput($nameArray, $array):void {
echo "{$nameArray} = [\n";
foreach ($array as $key => $value) {
echo "\t{$key} => {$value},\n";
}
echo "]\n";
}
$fruits = [
'One' => 'apple',
'Two' => 'banana',
'Three' => 'orange',
'Four' => 'grape',
'Five' => 'mango',
'Six' => 'pineapple',
'Seven' => 'strawberry',
'Eight' => 'watermelon'
];
arrayOutput("fruits", $fruits);
//echo "My first value is: " . $fruits[0] . "\n"; //Doesn't work
echo "My first value is: " . $fruits[array_keys($fruits)[0]] . "\n";
echo "My first value is: " . reset($fruits) . "\n"; //Does not modify the array's value
arrayOutput("fruits", $fruits);
//echo "My last value is: " . $fruits[$fruits[count($fruits) - 1]] . "\n";
echo "My last value is: " . $fruits[array_keys($fruits)[count($fruits) - 1]] . "\n";
echo "My last value is: " . end($fruits) . "\n"; //Does not modify the array's value
arrayOutput("fruits", $fruits);
$fruits = array_filter($fruits, function ($value) {
return strlen($value) > 5;
});
arrayOutput("fruits", $fruits);
$fruits = array_map(function ($value) {
return ucfirst($value);
}, $fruits);
arrayOutput("fruits", $fruits);