mirror of
https://github.com/php/doc-zh.git
synced 2026-03-26 08:02:16 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@95699 c90b9560-bf6c-de11-be94-00142212c4b1
125 lines
3.8 KiB
XML
Executable File
125 lines
3.8 KiB
XML
Executable File
<?xml version="1.0" encoding="gb2312"?>
|
||
<!-- $Revision: 1.2 $ -->
|
||
<!-- $Author: dallas $ -->
|
||
<!-- EN-Revision: 1.4 Maintainer: dallas Status: ready -->
|
||
<refentry id="function.array-walk">
|
||
<refnamediv>
|
||
<refname>array_walk</refname>
|
||
<refpurpose>
|
||
对数组中的每个成员应用用户函数
|
||
</refpurpose>
|
||
</refnamediv>
|
||
<refsect1>
|
||
<title>说明</title>
|
||
<methodsynopsis>
|
||
<type>int</type><methodname>array_walk</methodname>
|
||
<methodparam><type>array</type><parameter>array</parameter></methodparam>
|
||
<methodparam><type>string</type><parameter>func</parameter></methodparam>
|
||
<methodparam choice="opt"><type>mixed</type><parameter>userdata</parameter></methodparam>
|
||
</methodsynopsis>
|
||
<simpara>
|
||
对 <parameter>array</parameter> 数组的每个单元应用用户自定义函数
|
||
<parameter>func</parameter>。数组中的值作为第一个参数,键名作为第二个参数传入
|
||
<parameter>func</parameter> 函数。如果提供了
|
||
<parameter>userdata</parameter>,将会被作为第三个参数传入用户函数。<parameter>func</parameter>
|
||
必须是一个用户自定义函数,而不能是 PHP 本来有的函数。因此不能在 <function>array_walk</function>
|
||
中直接使用 <function>str2lower</function>,必须先建立一个用户自定义函数,再把这个函数名当作参数传递进去。
|
||
</simpara>
|
||
¬e.func-callback;
|
||
<simpara>
|
||
如果 <parameter>func</parameter> 需要超过两个或三个参数(根据
|
||
<parameter>userdata</parameter>),每次 <function>array_walk</function>
|
||
调用 <parameter>func</parameter> 都会产生一个警告。这些警告可以通过在
|
||
<function>array_walk</function> 调用的前面加上“@”符号来抑制住,或者通过 <function>error_reporting</function>。
|
||
</simpara>
|
||
<note>
|
||
<para>
|
||
如果 <parameter>func</parameter> 需要直接作用于数组中的值,则 <parameter>func</parameter>
|
||
定义中的第一个参数应该用引用传递。这样任何对这些单元的改变也将会改变数组本身。
|
||
</para>
|
||
<para>
|
||
在 <parameter>func</parameter> 中修改数组可能会产生不可预知的行为。
|
||
</para>
|
||
</note>
|
||
<note>
|
||
<para>
|
||
将键名和 userdata 传递到 <parameter>func</parameter> 中是 PHP 4.0 新增加的。
|
||
</para>
|
||
<para>
|
||
在 PHP 4 中需要调用 <function>reset</function>,因为
|
||
<function>array_walk</function> 默认不会重置数组。
|
||
</para>
|
||
<para>
|
||
用户最好不要在回调函数中修改数组本身。例如添加/删除单元,unset
|
||
<function>array_walk</function> 正在作用的数组。如果数组改变了,本函数的行为没有定义。
|
||
</para>
|
||
</note>
|
||
<para>
|
||
<example>
|
||
<title><function>array_walk</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
$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');
|
||
reset ($fruits);
|
||
array_walk ($fruits, 'test_alter', 'fruit');
|
||
echo "... and after:\n";
|
||
reset ($fruits);
|
||
array_walk ($fruits, 'test_print');
|
||
]]>
|
||
</programlisting>
|
||
<para>
|
||
以上程序输出为:
|
||
<screen role="php">
|
||
<![CDATA[
|
||
Before ...:
|
||
d. lemon
|
||
a. orange
|
||
b. banana
|
||
c. apple
|
||
... and after:
|
||
d. fruit: lemon
|
||
a. fruit: orange
|
||
b. fruit: banana
|
||
c. fruit: apple
|
||
]]>
|
||
</screen>
|
||
</para>
|
||
</example>
|
||
</para>
|
||
<simpara>
|
||
参见 <function>each</function> 和 <function>list</function>。
|
||
</simpara>
|
||
</refsect1>
|
||
</refentry>
|
||
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"../../../../manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|