1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files

190 lines
5.1 KiB
XML
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 2e60c5134e7a847c99f81eb3f7ecee1f5efeeace Maintainer: HonestQiao Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<refentry xml:id="function.each" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>each</refname>
<refpurpose>返回数组中当前的键/值对并将数组指针向前移动一步</refpurpose>
</refnamediv>
<refsynopsisdiv>
&warn.deprecated.function-7-2-0.removed-8-0-0;
</refsynopsisdiv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>each</methodname>
<methodparam><type class="union"><type>array</type><type>object</type></type><parameter role="reference">array</parameter></methodparam>
</methodsynopsis>
<para>
返回数组中当前的键/值对并将数组指针向前移动一步
</para>
<para>
在执行 <function>each</function>
之后,数组指针将停留在数组中的下一个单元或者当碰到数组结尾时停留在最后一个单元。如果要再用
each 遍历数组,必须使用 <function>reset</function>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<para>
输入的数组。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回 <parameter>array</parameter> 数组中当前指针位置的键/值对并向前移动数组指针。键值对被返回为四个单元的数组,键名为<literal>0</literal><literal>1</literal><literal>key</literal><literal>value</literal>。单元 <literal>0</literal>
<literal>key</literal> 包含有数组单元的键名,<literal>1</literal>
<literal>value</literal> 包含有数据。
</para>
<para>
如果内部指针越过了数组的末端,则 <function>each</function> 返回 &false;
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>each</function> 示例</title>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>
]]>
</programlisting>
<para>
<varname>$bar</varname> 现在包含有如下的键/值对:
</para>
<screen>
<![CDATA[
Array
(
[1] => bob
[value] => bob
[0] => 0
[key] => 0
)
]]>
</screen>
</example>
</para>
<para>
<informalexample>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each($foo);
print_r($bar);
?>
]]>
</programlisting>
<para>
<varname>$bar</varname> 现在包含有如下的键/值对:
</para>
<screen>
<![CDATA[
Array
(
[1] => Bob
[value] => Bob
[0] => Robert
[key] => Robert
)
]]>
</screen>
</informalexample>
</para>
<para>
<function>each</function> 经常和 <function>list</function>
结合使用来遍历数组,例如:
<example>
<title><function>each</function> 遍历数组</title>
<programlisting role="php" annotations="non-interactive">
<![CDATA[
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
a => apple
b => banana
c => cranberry
]]>
</screen>
</example>
</para>
<caution>
<para>
因为将一个数组赋值给另一个数组时会重置原来的数组指针,因此在上边的例子中如果我们在循环内部将
<varname>$fruit</varname> 赋给了另一个变量的话将会导致无限循环。
</para>
</caution>
<warning>
<para>
<function>each</function> 也接受对象,但可能会返回意外结果。因此不建议使用
<function>each</function> 遍历对象属性。
</para>
</warning>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>key</function></member>
<member><function>list</function></member>
<member><function>current</function></member>
<member><function>reset</function></member>
<member><function>next</function></member>
<member><function>prev</function></member>
<member>&foreach;</member>
<member><link linkend="language.oop5.iterations">遍历对象</link></member>
</simplelist>
</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:"~/.phpdoc/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
-->