What is the array_walk_recursive method in PHP?

As you learn and grow in programming, you encounter numerous functions and methods that help streamline your coding journey. One such powerful tool in PHP, a popular server-side scripting language, is the array_walk_recursive function. This function is essential when dealing with arrays in PHP, particularly nested or multidimensional ones. It allows you to modify each element of an array in place using a callback function, which transverses the array to its deepest level.

Think of array_walk_recursive as a tour guide leading you through the maze-like structure of a multidimensional array. It makes sure every "room" or value is visited, ensuring that no stone is left unturned.

Function syntax and parameters

Before we delve deeper, let's understand the syntax of array_walk_recursive. It is expressed as:

<?php
bool array_walk_recursive ( array &$array, callable $callback, mixed ...$userdata)
?>

This function takes three parameters:

  • $array (required): The input array, which is passed by reference.

  • $callback (required): The callback function to run for each element in the array. This function takes two parameters of its own which are the array item's value and key.

  • $userdata (optional): Additional parameters that the callback function can accept.

To visualize, let's consider a callback function named myfunction. It's defined as follows:

<?php
function myfunction($value, $key)
{
// perform operations on $value
}
?>

Applications

The real potential of array_walk_recursive comes to light when dealing with multidimensional arrays. It dives into the deepest level of the array to apply the callback function, something that the regular array_walk can’t do.

Consider the following multidimensional array:

<?php
$array = array(
"first" => "Tom",
"last" => "Hanks",
"films" => array(
"Saving Private Ryan",
"Cast Away",
"Forrest Gump",
),
);
?>

Now, imagine we want to change all the values to uppercase. We could use array_walk_recursive in this way:

<?php
function make_uppercase($value, $key)
{
return strtoupper($value);
}
array_walk_recursive($array, "make_uppercase");
?>

Considerations

While array_walk_recursive is powerful, it's important to understand its limitations and peculiarities. For instance, it only operates on the leaf nodes of the array—those without arrays of their own. If you need to manipulate every node, including those containing other arrays, array_walk might be a better fit.

Also, note that array_walk_recursive modifies the original array. If you need to preserve the original, consider creating a copy before applying this function.

Example

Now, let's see a practical example of using the array_walk_recursive function. In this example, we will apply a function to each element in a multidimensional array that will modify it.

<?php
// Defining the array
$array = array("a" => "apple", "b" => array("x" => "xerox", "y" => "yellow"));
// Defining the function that will be applied to each item
function test_alter(&$item, $key)
{
$item = "$key holds $item";
}
// Using array_walk_recursive to apply the function to each item
array_walk_recursive($array, 'test_alter');
// Printing the array to see the result
print_r($array);
?>
  • Line 3: We define a multidimensional array named $array.

  • Line 6–9: We define a function test_alter that takes two parameters: $item and $key.

  • Line 8: Inside the function, we modify $item to be a string that includes both the key and the original value.

  • Line 12: We use array_walk_recursive to apply test_alter to every item in $array. This effectively changes every item in the array.

  • Line 15: We print out the array. After array_walk_recursive has been applied, each item has been changed according to the test_alter function.

You can experiment with this code by changing the input array or modifying the function test_alter. Working with this example will help you get a feel for how array_walk_recursive works in different scenarios.

For e.g. you can try changing the function test_alter to do something else, like this:

<?php
function test_alter(&$item, $key)
{
$item = strtoupper($item);
}
?>

Conclusion

To sum up, the array_walk_recursive function is an essential utility in the array of PHP functions, particularly when interacting with multidimensional arrays. It empowers you to alter each element in an array by utilizing a callback function, guaranteeing that every element in a nested array is addressed.

Bear in mind the particular needs of your project when making a choice between array_walk and array_walk_recursive. Despite the fact that the latter delves into nested arrays more extensively, there may be instances where the former is a more suitable option. Wishing you an enjoyable coding journey!

To learn about more PHP functions:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved