array_walk vs array_walk_recursive: Whats the Difference (PHP Code Examples)

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Introduction

We have seen array_walk and array_walk_recursive functions already. In this article, we will know the difference between array_walk and array_walk_recursive. To get the most of this article, we recommend understanding these functions really well. The fundamental difference between these functions is subtle, and that is, array_walk doesn’t dig deep into multidimensional arrays.

Contrary to this, array_walk_recursive uses recursion to access the values in the innermost arrays of a multidimensional array. Alternatively, we can say that it accesses the values that are leaf nodes of the resulting tree structure.

Before we jump to the main article, let’s first review the array we will be using in the examples and its resulting tree structure.

Developers Array

$developers = [
    "PHP" => [
        "Nina" => ["Experience(Years)"=>5,"Skills"=>["PHP","Laravel","Git"]],
        "Frederick" => ["Experience(Years)"=>4,"Skills"=>["PHP","Laravel","MVC","Git"]],
    ],
    "Javascript" => [
        "Allen" => ["Experience(Years)"=>5,"Skills"=>["React","React Native","Git"]],
        "Monty" => ["Experience(Years)"=>4,"Skills"=>["Nodejs","Express","Nestjs","Git"]],
    ],
    "Java" => [
        "Franklin" => ["Experience(Years)"=>5,"Skills"=>["Java Spring","MVC","Git"]],
        "Daniel" => ["Experience(Years)"=>4,"Skills"=>["Android","Adobe XD","Git"]],
    ],
];

Tree Structure

array_walk and array_walk_recursive

Cool! We will use this tree illustration to highlight the accessed nodes.

Difference between array_walk and array_walk_recursive

The fundamental difference between the two functions is that array_walk_recursive access the leaf nodes while array_walk works at nodes at the top level. We will see that through an example and through the illustration too. Before that, let’s review the function signature and try to see if there are any differences.

Function Signature: array_walk

array_walk(array|object &$array, callable $callback, mixed $arg = null): bool

Function Signature: array_walk_recursive

array_walk_recursive(array|object &$array, callable $callback, mixed $arg = null): bool

So the function signatures are similar, which is why beginners often get confused between these two functions. But their difference is evident from their names. Let’s pass the developers array to array_walk and then to the array_walk recursive. We will simply log the results and see the output on the console.

Example: array_walk

array_walk($developers,function($v,$k){
    echo $k.': ';
    print_r($v).PHP_EOL;
});

Output

/*
OUTPUT
PHP: Array
(
    [Nina] => Array
        (
            [Experience(Years)] => 5
            [Skills] => Array      
                (
                    [0] => PHP      
                    [1] => Laravel  
                    [2] => Git      
                )
 
        )
 
    [Frederick] => Array
        (
            [Experience(Years)] => 4
            [Skills] => Array
                (
                    [0] => PHP
                    [1] => Laravel
                    [2] => MVC
                    [3] => Git
                )
 
        )
 
)
Javascript: Array
(
    [Allen] => Array
        (
            [Experience(Years)] => 5
            [Skills] => Array
                (
                    [0] => React
                    [1] => React Native
                    [2] => Git
                )
 
        )
 
    [Monty] => Array
        (
            [Experience(Years)] => 4
            [Skills] => Array
                (
                    [0] => Nodejs
                    [1] => Express
                    [2] => Nestjs
                    [3] => Git
                )
 
        )
 
)
Java: Array
(
    [Franklin] => Array
        (
            [Experience(Years)] => 5
            [Skills] => Array
                (
                    [0] => Java Spring
                    [1] => MVC
                    [2] => Git
                )
 
        )
 
    [Daniel] => Array
        (
            [Experience(Years)] => 4
            [Skills] => Array
                (
                    [0] => Android
                    [1] => Adobe XD
                    [2] => Git
                )
 
        )
 
)
*/

Here’s what it has accessed and logged to the console. We have highlighted that in the tree diagram as follows.

array_walk and array_walk_recursive

The array_walk accesses the top-level arrays and prints them out to the console. Next, we will repeat the same pattern for array_walk_recursive to mark the differences.

Example: array_walk_recursive

array_walk_recursive($developers,function($v,$k){
    echo $k.': '.$v.PHP_EOL;
   
});

Output

/*
OUTPUT
Experience(Years): 5
0: PHP
1: Laravel
2: Git
Experience(Years): 4
0: PHP
1: Laravel
2: MVC
3: Git
Experience(Years): 5
0: React
1: React Native    
2: Git
Experience(Years): 4
0: Nodejs
1: Express
2: Nestjs
3: Git
Experience(Years): 5
0: Java Spring
1: MVC
2: Git
Experience(Years): 4
0: Android
1: Adobe XD
2: Git
*/

So, let’s look at the tree diagram for clarity.

You can see that it relates to the innermost values. In tree diagram terminology, it accesses the leaf nodes only. 

Conclusion

So, in this brief article, we have seen the fundamental difference between array_walk and array_walk_recursive. The array_walk_recursive function recursively accesses a multidimensional array. It gets to the values located at the innermost arrays or the values at the leaf nodes. On the contrary, the array_walk just gets the top-level arrays without caring much about nested arrays. We hope you’ve learned this difference. Stay tuned for more informative PHP articles.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.