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

ParameterTypeWhat it takes
&$arrayarray|objectThe input array.
$callbackcallableTypically, callback takes on two parameters. The array parameter's value being the first, and the key/index second.
$argmixedIf 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

VersionChange
8.2.0The return type is true now; previously, it was bool.

See also

array_walk()

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.