PHP中的array_walk_recursive()函数
array_walk_recursice()函数将用户函数递归地应用于数组的每个成员。
语法
array_walk_recursive(arr, custom_func, parameter)
参数
arr-指定的数组。需要。
custom_func-用户定义的函数。需要。
参数-要为自定义功能设置的参数。可选的。
返回
array_walk_recursive()函数成功返回TRUE,失败返回FALSE。
示例
以下是一个例子-
<?php
function display($val,$key) {
echo "Key $key with the value $val<br>";
}
$arr1 = array("p"=>"accessories","q"=>"footwear");
$arr2 = array($arr1,"1"=>"electronics");
array_walk_recursive($arr2,"display");
?>输出结果
Key p with the value accessories Key q with the value footwear Key 1 with the value electronics
示例
让我们看另一个传递另一个参数的例子-
<?php
function display($val,$key, $extra) {
echo "Key $key $extra $val<br>";
}
$arr1 = array("p"=>"accessories","q"=>"footwear");
$arr2 = array($arr1,"5"=>"electronics");
array_walk_recursive($arr2,"display", "with value");
?>输出结果
Key p with the value accessories Key q with the value footwear Key 5 with the value electronics