The array_walk_recursive function iterates through elements of the array and passes each element’s key and value to a user-defined function. The keys and values of the array are the parameters of the user-defined function.
The difference between this function and the array_walk function is that it can work with the array inside an array.
We can change the value in the user-defined function by passing the first parameter ($value) as reference (&$value).
What is the syntax of the array_walk function in PHP?
array_walk_recursive(array, myfunction, parameter...)
Parameters | Details |
---|---|
array | The array to walking through – Required |
my_function | The user-defined function for array keys and values – Required |
parameter,… | Defines the parameters for the user-defined function – Optional |
Examples of array_walk_recursive function
Example 1. Example of array_walk_recursive function.
<?php
function my_function($value,$key)
{
echo "$key $value";
}
$array_1=array("a"=>"R","b"=>"G","c"=>"B");
$arra_2=array($array_1, "a"=>"B","b"=>"Y");
array_walk($array_2,"my_function");
?>