PHP | array_walk_recursive() Function

The array_walk_recursive() function is an inbuilt function in PHP. The array_walk_recursive() function walks through the entire array regardless of pointer position and applies a callback function or user-defined function to every element of the array recursively. The array element’s keys and values are parameters in the callback function. The difference between this function and the array_walk() function is that it will recurse into deeper arrays (an array inside an array).

Syntax:

boolean array_walk_recursive($array, myFunction, $extraParam)

Parameters: This function accepts three parameters as described below:



  1. $array: This is a mandatory parameter and specifies the input array.
  2. myFunction: This parameter specifies the name of the user-defined function and is also mandatory. The user-defined function generally excepts two parameters of which the first parameter represent the array’s values and the second parameter represents the corresponding keys.
  3. $extraparam: This is an optional parameter. It specifies an extra parameter to the user-defined function in addition to the two parameters, array keys and values.

Return value: This function returns a boolean value. It returns TRUE on success or FALSE on failure.

Below programs illustrate the array_walk_recursive() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate 
// array_walk_recursive() function
  
// user-defined callback function
function myFunction($value, $key)
{
    echo "The key $key has the value $value \n";
}
  
// Input arrays
$arr1=array("x"=>"india", "y"=>"Pakistan");
$arr2=array($arr1, "1"=>"China", "2"=>"Japan");
  
// calling array_walk_recursive() without
// extra parameter
array_walk_recursive($arr2, "myFunction");
  
?> 

chevron_right



Output:

The key x has the value india 
The key y has the value Pakistan 
The key 1 has the value China 
The key 2 has the value Japan

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate 
// array_walk_recursive() function
  
// user-defined callback function 
// with extra parameter
function myFunction($value, $key , $extraParam)
{
    echo "The key $key $extraParam $value \n";
}
  
// Input arrays
$arr1=array("x"=>"india", "y"=>"Pakistan");
$arr2=array($arr1, "1"=>"China", "2"=>"Japan");
  
// calling array_walk_recursive() with
// extra parameter
array_walk_recursive($arr2, "myFunction", "has the value");
  
?> 

chevron_right


Output:

The key x has the value india 
The key y has the value Pakistan 
The key 1 has the value China 
The key 2 has the value Japan

Reference:
http://php.net/manual/en/function.array-walk-recursive.php



My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.