array_map()

array_map() is a PHP native function that applies a closure to every element of an array.

array_map() uses the first argument as the callback, and the following subsequent arguments as one argument of that closure each. Any missing value in an array is assigned as null, while a missing argument is a fatal error.

array_map() only provides the value in the array, and not the related key: this should be done with array_walk().

<?php

    function square($x) { return $x * $x; }

    $array = [1,2,3];
    $squared = array_map(square(...), $array);
    // [1, 4, 9];

    function squareSum($x, $y) { return $x ** 2 + $y ** 2; }

    $squareSums = array_map(squareSum(...), [1, 2, 3], [4, 5]); // array_map uses the longest array

?>

Documentation

See also PHP array_map Function: How to Transform Arrays with Examples and PHP array_map for associative array.

Related : array_walk()