Skip to content

Commit be11870

Browse files
028-Using array functions (reset, end, array_filter, array_map)
1 parent 2fa0227 commit be11870

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
function arrayOutput($nameArray, $array):void {
3+
echo "{$nameArray} = [\n";
4+
foreach ($array as $key => $value) {
5+
echo "\t{$key} => {$value},\n";
6+
}
7+
echo "]\n";
8+
}
9+
10+
$fruits = [
11+
'One' => 'apple',
12+
'Two' => 'banana',
13+
'Three' => 'orange',
14+
'Four' => 'grape',
15+
'Five' => 'mango',
16+
'Six' => 'pineapple',
17+
'Seven' => 'strawberry',
18+
'Eight' => 'watermelon'
19+
];
20+
arrayOutput("fruits", $fruits);
21+
22+
//echo "My first value is: " . $fruits[0] . "\n"; //Doesn't work
23+
echo "My first value is: " . $fruits[array_keys($fruits)[0]] . "\n";
24+
echo "My first value is: " . reset($fruits) . "\n"; //Does not modify the array's value
25+
arrayOutput("fruits", $fruits);
26+
27+
//echo "My last value is: " . $fruits[$fruits[count($fruits) - 1]] . "\n";
28+
echo "My last value is: " . $fruits[array_keys($fruits)[count($fruits) - 1]] . "\n";
29+
echo "My last value is: " . end($fruits) . "\n"; //Does not modify the array's value
30+
arrayOutput("fruits", $fruits);
31+
32+
$fruits = array_filter($fruits, function ($value) {
33+
return strlen($value) > 5;
34+
});
35+
arrayOutput("fruits", $fruits);
36+
37+
$fruits = array_map(function ($value) {
38+
return ucfirst($value);
39+
}, $fruits);
40+
arrayOutput("fruits", $fruits);

0 commit comments

Comments
 (0)