Current Location: Home> Function Categories> array_walk_recursive

array_walk_recursive

Recursively apply user functions to each member in the array
Name:array_walk_recursive
Category:Array
Programming Language:php
One-line Description:Recursively apply user functions to each member in the array.

Definition and usage

The array_walk_recursive() function applies a user-defined function to each element in the array. In a function, the key name and key value of the array are parameters.

The difference between this function and array_walk() function is that it can manipulate deeper arrays (one array contains another array).

Example

Apply user-defined functions to each element in the array:

 <?php
function myfunction ( $value , $key )
{
echo "The value of the key $key is $value .<br>" ;
}
$a1 = array ( "a" => "red" , "b" => "green" ) ;
$a2 = array ( $a1 , "1" => "blue" , ​​"2" => "yellow" ) ;
array_walk_recursive ( $a2 , "myfunction" ) ;
?>

Try it yourself

grammar

 array_walk_recursive ( array , myfunction , parameter ... )
parameter describe
array Required. Specify array.
myfunction Required. The name of the user-defined function.
userdata ,... Optional. Specifies the parameters of the user-defined function. You can pass as many parameters as you like to this function.

illustrate

Similar to array_walk() function, array_walk_recursive() function applies a callback function to each element in the array. The difference is that if the elements in the original array are also arrays, the callback function will be called recursively, that is, recursively into a deeper array.

Typically, myfunction accepts two parameters. The value of the array parameter is the first and the key name is the second. If the optional parameter userdata is provided, it will be passed to the callback function as the third parameter.

If the callback function needs to act directly on the value in the array, you can specify the first parameter of the callback function as a reference, so that any changes to these units will also change the original array itself.

Similar Functions
Popular Articles