PHP's array_walk() Function: How to Use it for Efficient Array Manipulation
Learn how PHP's array_walk() applies a callback to every array element in place, with syntax, examples, gotchas, and a comparison to array_map().
array_walk() is a built-in PHP function that applies a callback to every element of an array, in place. Unlike array_map(), which builds and returns a new array, array_walk() walks the existing array and lets you modify each element directly through a reference. This page covers its syntax, when to reach for it instead of a plain foreach loop or array_map(), the common gotchas around modifying values, and how to walk nested arrays with array_walk_recursive().
Syntax
array_walk(array &$array, callable $callback, mixed $arg = null): true| Parameter | Required | Description |
|---|---|---|
$array | Yes | The array to iterate over. It is passed by reference, so the callback can change its elements. |
$callback | Yes | A callable run once per element. Its signature is callback($value, $key, $arg). To modify an element, declare the first parameter by reference: function (&$value) { ... }. |
$arg | No | An optional extra argument passed as the third parameter to every callback call. |
array_walk() always returns true (it returns false only on a type error). It does not return the modified array — the array you passed in is changed directly.
Why pass
$valueby reference? Without the&, the callback receives a copy of each element, so any change is thrown away. Add&(as infunction (&$value)) and your edits are written back into the original array. See PHP functions and callable for more on callbacks.
Examples of using array_walk()
Example 1: Modifying Arrays
One of the primary uses of array_walk() is to modify arrays. With its ability to apply a user-defined function to each element of an array, it's an easy way to transform an array to fit your specific needs. Here is an example of how you can use array_walk() to change the case of all the elements in an array.
PHP Example 1: Modifying Arrays
Output:
Array
(
[0] => APPLE
[1] => BANANA
[2] => CHERRY
)In this example, we first define an array with three elements. We then define a user-defined function called change_case() that uses the PHP built-in function strtoupper() to change the case of each element to uppercase. We then use the array_walk() function to apply the change_case() function to each element of the array. Finally, we use the print_r() function to output the modified array.
Example 2: Performing Calculations
Another powerful use of array_walk() is to perform calculations on arrays. With its ability to apply a user-defined function to each element of an array, you can use array_walk() to perform a wide range of calculations on your arrays. Here is an example of how you can use array_walk() to calculate the sum of all the elements in an array.
Example 2: Performing Calculations
Output:
The total sum is: 15In this example, we first define an array with five elements. We then define an anonymous function that adds each element to a running total. We use the array_walk() function to apply the function to each element of the array. Finally, we output the total sum using the echo statement.
Example 3: Walking Multidimensional Arrays
Plain array_walk() only visits the top-level elements, so on a nested array your callback would receive the inner arrays rather than the leaf values. For nested data, use the companion function array_walk_recursive(), which descends into sub-arrays and runs your callback on every leaf value. Here we uppercase every string in a two-level array.
PHP Example 3: Working with Multidimensional Arrays
Output:
Array
(
[0] => Array
(
[0] => APPLE
[1] => BANANA
[2] => CHERRY
)
[1] => Array
(
[0] => ORANGE
[1] => GRAPE
[2] => PINEAPPLE
)
)array_walk_recursive() walks the structure depth-first and calls change_case() on each leaf string, leaving the nested shape of the array intact while uppercasing every value.
Common gotchas
- Forgetting the
&. If your callback's first parameter is not by reference (function (&$value)), your modifications are silently discarded. This is the single most common mistake witharray_walk(). - Returning a value does nothing.
array_walk()ignores whatever your callback returns. It only writes back changes made through the reference parameter. If you want to build a transformed copy instead, usearray_map(). - Callback parameter order is
($value, $key)— value first, then key. This is the opposite of what some developers expect. - You can't add or remove keys. Setting
$array = nullinside the callback, or changing the array's structure during the walk, leads to undefined behaviour. Usearray_walk()only to transform values in place.
array_walk() vs. array_map() vs. foreach
| Tool | Modifies in place? | Returns | Best for |
|---|---|---|---|
array_walk() | Yes (via reference) | true | Mutating an existing array, or running a side effect per element |
array_map() | No | A new array | Producing a transformed copy without touching the original |
foreach | Yes (with &$value) | — | General iteration with break/continue and full control flow |
array_reduce() | No | A single value | Collapsing an array down to one result (sum, concatenation, etc.) |
Reach for array_walk() when you want to keep the same array and change its values in place, or when you need each callback to receive both the value and the key. Choose array_map() when you want a fresh array and to leave the source untouched.
Conclusion
array_walk() applies a callback to every element of an array in place, making it a clean way to transform values, run per-element side effects, or process key/value pairs without writing an explicit loop. Remember to take the value by reference when you intend to modify it, reach for array_walk_recursive() on nested arrays, and prefer array_map() when you'd rather build a new array than mutate the original. For more on iterating over arrays, see PHP arrays and the foreach loop.