mirror of
https://github.com/php/doc-zh.git
synced 2026-03-27 16:42:13 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@95699 c90b9560-bf6c-de11-be94-00142212c4b1
111 lines
3.6 KiB
XML
Executable File
111 lines
3.6 KiB
XML
Executable File
<?xml version="1.0" encoding="gb2312"?>
|
||
<!-- $Revision: 1.2 $ -->
|
||
<!-- $Author: dallas $ -->
|
||
<!-- EN-Revision: 1.6 Maintainer: dallas Status: ready -->
|
||
<refentry id="function.each">
|
||
<refnamediv>
|
||
<refname>each</refname>
|
||
<refpurpose>
|
||
返回数组中当前的键/值对并将数组指针向前移动一步
|
||
</refpurpose>
|
||
</refnamediv>
|
||
<refsect1>
|
||
<title>说明</title>
|
||
<methodsynopsis>
|
||
<type>array</type><methodname>each</methodname>
|
||
<methodparam><type>array</type><parameter>array</parameter></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
返回 <parameter>array</parameter> 数组中当前指针位置的键/值对并向前移动数组指针。键值对被返回为四个单元的数组,键名为
|
||
<emphasis>0</emphasis>,<emphasis>1</emphasis>,<emphasis>key</emphasis>
|
||
和 <emphasis>value</emphasis>。单元 <emphasis>0</emphasis> 和
|
||
<emphasis>key</emphasis> 包含有数组单元的键名,<emphasis>1</emphasis> 和
|
||
<emphasis>value</emphasis> 包含有数据。
|
||
</para>
|
||
<para>
|
||
如果内部指针越过了数组的末端,则 <function>each</function> 返回 &false;。
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title><function>each</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
$foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese");
|
||
$bar = each ($foo);
|
||
]]>
|
||
</programlisting>
|
||
<para>
|
||
<varname>$bar</varname> 现在包含有如下的键/值对:
|
||
<itemizedlist spacing="compact">
|
||
<listitem><simpara>0 => 0</simpara></listitem>
|
||
<listitem><simpara>1 => 'bob'</simpara></listitem>
|
||
<listitem><simpara>key => 0</simpara></listitem>
|
||
<listitem><simpara>value => 'bob'</simpara></listitem>
|
||
</itemizedlist>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
$foo = array ("Robert" => "Bob", "Seppo" => "Sepi");
|
||
$bar = each ($foo);
|
||
]]>
|
||
</programlisting>
|
||
</para>
|
||
<para>
|
||
<varname>$bar</varname> 现在包含有如下的键/值对:
|
||
<itemizedlist spacing="compact">
|
||
<listitem><simpara>0 => 'Robert'</simpara></listitem>
|
||
<listitem><simpara>1 => 'Bob'</simpara></listitem>
|
||
<listitem><simpara>key => 'Robert'</simpara></listitem>
|
||
<listitem><simpara>value => 'Bob'</simpara></listitem>
|
||
</itemizedlist>
|
||
</para>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<function>each</function> 经常和 <function>list</function>
|
||
结合使用来遍历数组,例如 <varname>$_POST</varname>:
|
||
<example>
|
||
<title>
|
||
用 <function>each</function> 遍历 <varname>$_POST</varname>:
|
||
|
||
</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
echo "Values submitted via POST method:<br />\n";
|
||
reset ($_POST);
|
||
while (list ($key, $val) = each ($_POST)) {
|
||
echo "$key => $val<br />\n";
|
||
}
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
在执行 <function>each</function> 之后,数组指针将停留在数组中的下一个单元或者当碰到数组结尾时停留在最后一个单元。如果要再用 each 遍历数组,必须使用 <function>reset</function>。
|
||
</para>
|
||
<para>
|
||
参见 <function>key</function>,<function>list</function>,<function>current</function>,<function>reset</function>,<function>next</function>,<function>prev</function> 和 <function>foreach</function>。
|
||
</para>
|
||
</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
|
||
-->
|