参数 | 描述 |
---|---|
array | 必需。规定数组。 |
function | 必需。用户自定义函数的名称。 |
userdata | 可选。用户输入的值,可作为回调函数的参数。 |
提示:您可以为函数设置一个或多个参数。
注释:如果回调函数需要直接作用于数组中的值,可以将回调函数的第一个参数指定为引用:&$value。(参见例子 3)
注释:将键名和 userdata 传递到 function 中是 PHP 4.0 新增加的。
<?php function myfunction($value,$key) { echo "The key $key has the value $value<br />"; } $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse"); array_walk($a,"myfunction"); ?>
输出:
The key a has the value Cat The key b has the value Dog The key c has the value Horse
带有一个参数:
<?php function myfunction($value,$key,$p) { echo "$key $p $value<br />"; } $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse"); array_walk($a,"myfunction","has the value"); ?>
输出:
a has the value Cat b has the value Dog c has the value Horse
改变数组元素的值(请注意 &$value):
<?php function myfunction(&$value,$key) { $value="Bird; } $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse"); array_walk($a,"myfunction"); print_r($a); ?>
输出:
Array ( [a] => Bird [b] => Bird [c] => Bird )
制作:
http://www.7di.net 生活网搜集整理 参考: http://www.w3school.com.cn/php/func_array_walk.asp |