Skip to content
Justice Tech School

array_map, array_filter, array_reduce

Transform, select and fold arrays with callbacks.

Example
$n = [1, 2, 3, 4];
print_r(array_map(fn($x) => $x * 2, $n));
print_r(array_filter($n, fn($x) => $x % 2 == 0));
echo array_reduce($n, fn($c, $x) => $c + $x, 0);

Result

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
)
Array
(
    [1] => 2
    [3] => 4
)
10

Exercise

Select items.

array_($a, $fn)

Quiz Test

What does array_map return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.