PHP array_walk_recursive() 函数
实例
对数组中的每个元素应用用户自定义函数:
<?php function myfunction($value,$key) { echo "键 $key 的值是 $value 。<br>"; } $a1=array("a"=>"red","b"=>"green"); $a2=array($a1,"1"=>"blue","2"=>"yellow"); array_walk_recursive($a2,"myfunction"); ?>
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 user-defined function parameters. 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 element in the original array is also an array, 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+ |