array_walk_recursive() applies a callback to every non-array value inside a nested array. It descends through nested arrays automatically.
$data = ['a' => 1, 'group' => ['b' => 2]];
array_walk_recursive($data, function (&$value) {
$value *= 10;
});
print_r($data);The callback receives leaf values; nested arrays themselves are not passed to it as leaf values.
array_walk_recursive(array $array, callable $callback, mixed $userdata = null): bool
function display($value, $key) {
echo "$key: $value <BR>";
}
$array = ['a' => 1, 'b' => 2];
array_walk_recursive($array, 'display');
a: 1
b: 2
function display($value, $key) {
echo "$key: $value <BR>";
}
$array = [
'a' => 1,
'b' => ['c' => 2, 'd' => 3]
];
array_walk_recursive($array, 'display');
a: 1
c: 2
d: 3
$array = [
'a' => 1,
'b' => ['c' => 2, 'd' => 3]
];
$prefix = "Prefix";
array_walk_recursive($array, function($value, $key) use ($prefix) {
echo "$prefix $key: $value <br>";
});
Prefix a: 1
Prefix c: 2
Prefix d: 3
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.