array_walk_recursive()
array_walk_recursive() — Apply a user function recursively to every member of an array.
Built-in function of PHP 8.5. The manual lists it as available in 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
Applies the user-defined callback function to each element of the array. This function will recurse into deeper arrays.
Signature
array_walk_recursive(array|object &$array, callable $callback, mixed $arg = null): true
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| &$array | array|object | The input array. |
| $callback | callable | Typically, callback takes on two parameters. The array parameter's value being the first, and the key/index second. |
| $arg | mixed | If the optional arg parameter is supplied, it will be passed as the third parameter to the callback. |
Return value
Always returns true.
Example
A runnable snippet:
$sweet = array('a' => 'apple', 'b' => 'banana'); $fruits = array('sweet' => $sweet, 'sour' => 'lemon'); function test_print($item, $key) { echo "$key holds $item\n"; } array_walk_recursive($fruits, 'test_print');
Output:
a holds apple b holds banana sour holds lemon
Changelog
| Version | Change |
|---|---|
| 8.2.0 | The return type is true now; previously, it was bool. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_walk_recursive() (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.