Home >>PHP Array Functions >PHP array_walk_recursive() Function
PHP array_walk_recursive() function is used to run each elements of the given input array in to a user-defined function recursively. The keys and the values of the array are used as the parameter for the user-defined function. It is similar to the array_walk() function but the difference is that it passes the elements to the user-defined function recursively. It returns a Boolean value TRUE on success and False on failure.
Syntax:
array_walk_recursive(array, function, parameter...);
Parameter | Description |
---|---|
array | This is a requiredparameter.This parameter definesthe given array. |
function | This is a requiredparameter.This parameter definesthe name of the user-defined function. |
parameter,... | This is anoptionalparameter.This parameter definesa parameter to the user-defined function. |
Here is an example of array_walk_recursive() function in PHP:
<html> <body> <?php function myfunction($value,$key) { echo "$key has the value $value<br>"; } $x=array("a"=>"1","b"=>"2","c"=>"3"); $y=array($x,"d"=>"4","e"=>"44"); array_walk_recursive($y,"myfunction"); ?> </body> </html>
a has the value 1 b has the value 2 c has the value 3 d has the value 4 e has the value 44
Example 2:
<html> <body> <?php function myfunction($value,$key,$p) { echo "$key $p $value<br>"; } $x=array("a"=>"1","b"=>"2","c"=>"3"); $y=array($x,"d"=>"4","e"=>"44"); array_walk_recursive($y,"myfunction","has the value"); ?> </body> </html>
a has the value 1 b has the value 2 c has the value 3 d has the value 4 e has the value 44