php array_walk_recursive
时间: 2023-09-18 22:16:21 浏览: 47
The `array_walk_recursive` function in PHP is used to apply a user-defined function to each element of an array recursively. This means that it will traverse through all nested arrays and apply the function to each element.
Syntax:
```
array_walk_recursive(array $array, callable $function [, mixed $userdata = NULL ])
```
Parameters:
- `$array` - Required. Specifies the array to be walked recursively.
- `$function` - Required. Specifies the user-defined function to be applied to each element of the array recursively.
- `$userdata` - Optional. Specifies additional user-defined data to be passed to the function.
Return Value:
The `array_walk_recursive` function does not return anything. It modifies the original array by applying the user-defined function to each element.
Example:
```
function addOne(&$value, $key) {
$value += 1;
}
$array = array(1, 2, array(3, 4, array(5, 6)));
array_walk_recursive($array, 'addOne');
print_r($array);
```
Output:
```
Array
(
[0] => 2
[1] => 3
[2] => Array
(
[0] => 4
[1] => 5
[2] => Array
(
[0] => 6
[1] => 7
)
)
)
```
In this example, the `addOne` function is defined to add 1 to each element. The `array_walk_recursive` function is then used to apply this function to each element of the array recursively. The output shows that each element has been incremented by 1.