PHP array_walk_recursive Function
last modified March 13, 2025
The PHP array_walk_recursive
function applies a user function
recursively to every element of an array. It's useful for nested arrays.
Basic Definition
array_walk_recursive
processes arrays recursively, applying a
callback to each non-array element. It preserves array keys during traversal.
Syntax: array_walk_recursive(array &$array, callable $callback, mixed $userdata = null): bool
.
The callback receives value and key parameters.
Basic array_walk_recursive Example
This example demonstrates printing all values from a nested array structure.
<?php declare(strict_types=1); $data = [ 'name' => 'John', 'contacts' => [ 'email' => 'john@example.com', 'phone' => '123456789' ], 'age' => 30 ]; array_walk_recursive($data, function($value, $key) { echo "$key: $value\n"; });
The callback function receives each key-value pair. Nested arrays are processed recursively, but their parent keys aren't passed to the callback.
Modifying Array Values
This example shows how to modify array values in place using references.
<?php declare(strict_types=1); $numbers = [ 'a' => 1, 'b' => [2, 3], 'c' => 4 ]; array_walk_recursive($numbers, function(&$value, $key) { $value *= 2; }); print_r($numbers);
By passing the value by reference (&$value), we can modify the original array elements. All numeric values are doubled in this example.
Using User Data Parameter
The third parameter allows passing additional data to the callback function.
<?php declare(strict_types=1); $products = [ 'item1' => ['price' => 100, 'quantity' => 2], 'item2' => ['price' => 200, 'quantity' => 1] ]; $discount = 0.1; // 10% discount array_walk_recursive($products, function(&$value, $key, $discount) { if ($key === 'price') { $value *= (1 - $discount); } }, $discount); print_r($products);
The discount value is passed as user data and applied only to price fields. This demonstrates selective modification based on both key and user data.
Counting Array Elements
This example counts all non-array elements in a multidimensional array.
<?php declare(strict_types=1); $data = [ 'a' => 1, 'b' => [2, 3, [4, 5]], 'c' => 6 ]; $count = 0; array_walk_recursive($data, function($value) use (&$count) { $count++; }); echo "Total elements: $count";
Using a closure with variable binding, we increment a counter for each non-array element. The result shows the total count of leaf nodes.
Building a Flat Array
This example collects all values from a nested array into a flat array.
<?php declare(strict_types=1); $nested = [ 'a' => 1, 'b' => [ 'c' => 2, 'd' => [3, 4] ], 'e' => 5 ]; $flat = []; array_walk_recursive($nested, function($value) use (&$flat) { $flat[] = $value; }); print_r($flat);
By appending each value to an external array, we create a flattened version. This technique is useful when you need to process all values uniformly.
Best Practices
- Reference Parameters: Use &$value to modify array elements.
- Key Awareness: Check $key when you need conditional logic.
- User Data: Pass additional data via the third parameter.
- Closures: Use 'use' keyword to access external variables.
- Performance: Avoid complex operations in large arrays.
Source
PHP array_walk_recursive Documentation
This tutorial covered the PHP array_walk_recursive
function with
practical examples showing its usage for processing nested array structures.
Author
List all PHP Array Functions.