(PHP 4, PHP 5)
array_walk — 对数组中的每个成员应用用户函数
成功时返回 TRUE
, 或者在失败时返回 FALSE
.
将用户自定义函数 funcname
应用到
array
数组中的每个单元。典型情况下
funcname
接受两个参数。array
参数的值作为第一个,键名作为第二个。如果提供了可选参数
userdata
,将被作为第三个参数传递给
callback funcname
。
如果 funcname
函数需要的参数比给出的多,则每次
array_walk() 调用 funcname
时都会产生一个 E_WARNING
级的错误。这些警告可以通过在 array_walk() 调用前加上
PHP 的错误操作符 @
来抑制,或者用 error_reporting()。
Note:
如果
funcname
需要直接作用于数组中的值,则给funcname
的第一个参数指定为引用。这样任何对这些单元的改变也将会改变原始数组本身。
Note:
将键名和 userdata 传递到
funcname
中是 PHP 4.0 新增加的。
array_walk() 不会受到 array
内部数组指针的影响。array_walk()
会遍历整个数组而不管指针的位置。
用户不应在回调函数中改变该数组本身。例如增加/删除单元,unset 单元等等。如果 array_walk() 作用的数组改变了,则此函数的的行为未经定义,且不可预期。
Example #1 array_walk() 例子
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
echo "Before ...:\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
?>
以上例程会输出:
Before ...: d. lemon a. orange b. banana c. apple ... and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple
参见 array_walk_recursive(),create_function(),list(),foreach,each(),call_user_func_array(),array_map() 和callback 类型的信息。
array
The input array.
funcname
Typically, funcname
takes on two parameters.
The array
parameter's value being the first, and
the key/index second.
Note:
If
funcname
needs to be working with the actual values of the array, specify the first parameter offuncname
as a reference. Then, any changes made to those elements will be made in the original array itself.
Note:
Many internal functions (for example strtolower()) will throw a warning if more than the expected number of argument are passed in and are not usable directly as
funcname
.
Only the values of the array
may potentially be
changed; its structure cannot be altered, i.e., the programmer cannot
add, unset or reorder elements. If the callback does not respect this
requirement, the behavior of this function is undefined, and
unpredictable.
userdata
If the optional userdata
parameter is supplied,
it will be passed as the third parameter to the callback
funcname
.
成功时返回 TRUE
, 或者在失败时返回 FALSE
.
If function funcname
requires more parameters than
given to it, an error of level
E_WARNING will be generated each time array_walk()
calls funcname
. These warnings may be suppressed by
prepending the PHP error operator
@ to the
array_walk() call, or by using
error_reporting().
Example #2 array_walk() example
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
echo "Before ...:\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
?>
以上例程会输出:
Before ...: d. lemon a. orange b. banana c. apple ... and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple