2002-05-12 08:19:28 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2004-08-03 17:05:01 +00:00
|
|
|
<!-- $Revision: 1.15 $ -->
|
2002-04-15 00:12:54 +00:00
|
|
|
<!-- splitted from ./en/functions/array.xml, last change in rev 1.2 -->
|
|
|
|
<refentry id="function.each">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>each</refname>
|
|
|
|
<refpurpose>
|
|
|
|
Return the current key and value pair from an array and advance
|
|
|
|
the array cursor
|
|
|
|
</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>array</type><methodname>each</methodname>
|
2004-08-03 17:05:01 +00:00
|
|
|
<methodparam><type>array</type><parameter>&array</parameter></methodparam>
|
2002-04-15 00:12:54 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
|
|
|
Returns the current key and value pair from the array
|
|
|
|
<parameter>array</parameter> and advances the array cursor. This
|
|
|
|
pair is returned in a four-element array, with the keys
|
2004-05-04 19:56:18 +00:00
|
|
|
<literal>0</literal>, <literal>1</literal>,
|
|
|
|
<literal>key</literal>, and
|
|
|
|
<literal>value</literal>. Elements <literal>0</literal> and
|
|
|
|
<literal>key</literal> contain the key name of the array
|
|
|
|
element, and <literal>1</literal> and
|
|
|
|
<literal>value</literal> contain the data.
|
2002-04-15 00:12:54 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
If the internal pointer for the array points past the end of the
|
|
|
|
array contents, <function>each</function> returns
|
|
|
|
&false;.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title><function>each</function> examples</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2003-05-30 18:12:53 +00:00
|
|
|
<?php
|
2003-08-17 12:21:03 +00:00
|
|
|
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
|
|
|
|
$bar = each($foo);
|
2003-02-04 17:19:32 +00:00
|
|
|
print_r($bar);
|
2003-05-30 18:12:53 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
<varname>$bar</varname> now contains the following key/value
|
|
|
|
pairs:
|
2003-06-16 12:03:39 +00:00
|
|
|
</para>
|
2003-02-04 17:19:32 +00:00
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[1] => bob
|
|
|
|
[value] => bob
|
|
|
|
[0] => 0
|
|
|
|
[key] => 0
|
|
|
|
)
|
|
|
|
]]>
|
|
|
|
</screen>
|
2003-06-16 12:03:39 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<informalexample>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-30 18:12:53 +00:00
|
|
|
<?php
|
2003-08-17 12:21:03 +00:00
|
|
|
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
|
|
|
|
$bar = each($foo);
|
2003-02-04 17:19:32 +00:00
|
|
|
print_r($bar);
|
2003-05-30 18:12:53 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2003-06-16 12:03:39 +00:00
|
|
|
</programlisting>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
|
|
|
<varname>$bar</varname> now contains the following key/value
|
|
|
|
pairs:
|
2003-06-16 12:03:39 +00:00
|
|
|
</para>
|
2003-02-04 17:19:32 +00:00
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[1] => Bob
|
|
|
|
[value] => Bob
|
|
|
|
[0] => Robert
|
|
|
|
[key] => Robert
|
|
|
|
)
|
|
|
|
]]>
|
|
|
|
</screen>
|
2003-06-16 12:03:39 +00:00
|
|
|
</informalexample>
|
2002-04-15 00:12:54 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<function>each</function> is typically used in conjunction with
|
2003-06-22 03:09:45 +00:00
|
|
|
<function>list</function> to traverse an array, here's an
|
|
|
|
example:
|
2002-04-15 00:12:54 +00:00
|
|
|
<example>
|
2003-06-22 03:09:45 +00:00
|
|
|
<title>Traversing an array with <function>each</function></title>
|
2002-04-15 00:12:54 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2003-05-30 18:12:53 +00:00
|
|
|
<?php
|
2003-06-22 03:09:45 +00:00
|
|
|
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
|
|
|
|
|
2003-08-17 12:21:03 +00:00
|
|
|
reset($fruit);
|
|
|
|
while (list($key, $val) = each($fruit)) {
|
2003-06-22 03:09:45 +00:00
|
|
|
echo "$key => $val\n";
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
2003-12-15 16:55:22 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
Outputs:
|
|
|
|
</para>
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
2003-06-22 03:09:45 +00:00
|
|
|
a => apple
|
|
|
|
b => banana
|
|
|
|
c => cranberry
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2003-12-15 16:55:22 +00:00
|
|
|
</screen>
|
2002-04-15 00:12:54 +00:00
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
After <function>each</function> has executed, the array cursor
|
2003-06-22 03:09:45 +00:00
|
|
|
will be left on the next element of the array, or past the last
|
2002-04-15 00:12:54 +00:00
|
|
|
element if it hits the end of the array. You have to use
|
|
|
|
<function>reset</function> if you want to traverse the array
|
|
|
|
again using each.
|
|
|
|
</para>
|
2003-06-22 03:09:45 +00:00
|
|
|
<caution>
|
|
|
|
<para>
|
|
|
|
Because assigning an array to another variable resets the original
|
|
|
|
arrays pointer, our example above would cause an endless loop had we
|
|
|
|
assigned <varname>$fruit</varname> to another variable inside the
|
|
|
|
loop.
|
|
|
|
</para>
|
|
|
|
</caution>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
|
|
|
See also <function>key</function>, <function>list</function>,
|
|
|
|
<function>current</function>, <function>reset</function>,
|
2002-04-18 23:15:34 +00:00
|
|
|
<function>next</function>, <function>prev</function>, and
|
2002-11-26 19:14:06 +00:00
|
|
|
<link linkend="control-structures.foreach">foreach</link>.
|
2002-04-15 00:12:54 +00:00
|
|
|
</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
|
|
|
|
-->
|