Array Transformation Functions
Apply array_map, array_filter, and array_reduce.
Functional Array Transforms
PHP provides three powerful higher-order functions for transforming arrays without explicit loops:
array_map()— transform each elementarray_filter()— keep elements that pass a testarray_reduce()— reduce to a single value
array_map()
Apply a callback to every element and return a new array:
<?php
$prices = [10, 20, 30, 40];
$doubled = array_map(fn($p) => $p * 2, $prices);
print_r($doubled); // [20, 40, 60, 80]
// Multiple arrays
$a = [1, 2, 3];
$b = [10, 20, 30];
$sums = array_map(fn($x, $y) => $x + $y, $a, $b);
print_r($sums); // [11, 22, 33]All lessons in this course
- Indexed and Associative Arrays
- Multidimensional Arrays
- Sorting and Searching Arrays
- Array Transformation Functions