PHP array_walk_recursive() Function

Example

Apply a user-defined function to each element in the array:

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

Run Example

Definition and Usage

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

This function is similar to array_walk() The difference of the function is that it can operate on deeper arrays (an array contains another array).

Syntax

array_walk_recursive(array,myfunction,parameter...)
Parameters Description
array Required. Specifies the array.
myfunction Required. Specifies the name of the user-defined function.
userdata,... Optional. Specifies the parameters of the user-defined function. You can pass any number of parameters to this function.

Description

With array_walk() The function is similar, array_walk_recursive() function applies the 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 recursively called, that is, it will recursively go to a deeper array.

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

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

Technical Details

Return Value: Returns TRUE if successful, otherwise returns FALSE.
PHP Version: 5+