W3docs

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
ParameterRequiredDescription
$arrayYesThe array to iterate over. It is passed by reference, so the callback can change its elements.
$callbackYesA callable run once per element. Its signature is callback($value, $key, $arg). To modify an element, declare the first parameter by reference: function (&$value) { ... }.
$argNoAn 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 $value by reference? Without the &, the callback receives a copy of each element, so any change is thrown away. Add & (as in function (&$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

php— editable, runs on the server

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

php— editable, runs on the server

Output:

The total sum is: 15

In 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

php— editable, runs on the server

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 with array_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, use array_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 = null inside the callback, or changing the array's structure during the walk, leads to undefined behaviour. Use array_walk() only to transform values in place.

array_walk() vs. array_map() vs. foreach

ToolModifies in place?ReturnsBest for
array_walk()Yes (via reference)trueMutating an existing array, or running a side effect per element
array_map()NoA new arrayProducing a transformed copy without touching the original
foreachYes (with &$value)General iteration with break/continue and full control flow
array_reduce()NoA single valueCollapsing 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.

Practice

Practice
What is the functionality of the 'array_walk()' function in PHP?
What is the functionality of the 'array_walk()' function in PHP?
Was this page helpful?