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:
- $array: This is a mandatory parameter and specifies the input array.
- 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.
- $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:
<?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"); ?> |
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:
<?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"); ?> |
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
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | Ds\Map xor() Function
- D3.js | d3.map.get() Function
- PHP | Ds\Map put() Function
- D3.js | d3.map.set() Function
- PHP | Ds\Map first() Function
- p5.js | sin() function
- PHP Ds\Map sum() Function
- p5.js | tan() function
- D3.js | d3.map.has() Function
- D3.js | d3.max() function
- p5.js | min() function
- PHP | Ds\Set last() Function
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.



