array_map()
array_map() — Applies the callback to the elements of the given arrays.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
array_map() returns an array containing the results of applying the callback to the corresponding value of array (and arrays if more arrays are provided) used as arguments for the callback. The number of parameters that the callback function accepts should match the number of arrays passed to array_map(). Excess input arrays are ignored. An ArgumentCountError is thrown if an insufficient number of arguments is provided.
Signature
array_map(?callable $callback, array $array, array ...$arrays): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $callback | ?callable | A callable to run for each element in each array. |
| $array | array | An array to run through the callback function. |
| ...$arrays | array | Supplementary variable list of array arguments to run through the callback function. |
Return value
Returns an array containing the results of applying the callback function to the corresponding value of array (and arrays if more arrays are provided) used as arguments for the callback. The returned array will preserve the keys of the array argument if and only if exactly one array is passed. If more than one array is passed, the returned array will have sequential integer keys.
Example
A runnable snippet:
function cube($n) { return ($n * $n * $n); } $a = [1, 2, 3, 4, 5]; $b = array_map('cube', $a); print_r($b);
Output:
Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )
Changelog
| Version | Change |
|---|---|
| 8.0.0 | If callback expects a parameter to be passed by reference, this function will now emit an E_WARNING. |
See also
array_filter(), array_reduce(), array_walk()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_map() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.