philip Sat Jun 21 23:09:46 2003 EDT Modified files: /phpdoc/en/reference/array/functions each.xml Log: Added a <caution> regarding bug #23195 as copying an array resets the original array's pointer which can cause an endless loop. Also, each() will move the pointer past the end as opposed to the end. Updated the example to be self-contained, no longer uses $_POST. Index: phpdoc/en/reference/array/functions/each.xml diff -u phpdoc/en/reference/array/functions/each.xml:1.10 phpdoc/en/reference/array/functions/each.xml:1.11 --- phpdoc/en/reference/array/functions/each.xml:1.10 Mon Jun 16 08:03:39 2003 +++ phpdoc/en/reference/array/functions/each.xml Sat Jun 21 23:09:45 2003 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.10 $ --> +<!-- $Revision: 1.11 $ --> <!-- splitted from ./en/functions/array.xml, last change in rev 1.2 --> <refentry id="function.each"> <refnamediv> @@ -90,21 +90,27 @@ </para> <para> <function>each</function> is typically used in conjunction with - <function>list</function> to traverse an array; for instance, - <varname>$_POST</varname>: + <function>list</function> to traverse an array, here's an + example: <example> - <title> - Traversing <varname>$_POST</varname> with - <function>each</function> - </title> + <title>Traversing an array with <function>each</function></title> <programlisting role="php"> <![CDATA[ <?php -echo "Values submitted via POST method:<br />\n"; -reset ($_POST); -while (list ($key, $val) = each ($_POST)) { - echo "$key => $val<br />\n"; +$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); + +reset ($fruit); +while (list ($key, $val) = each ($fruit)) { + echo "$key => $val\n"; } + +/* Outputs: + +a => apple +b => banana +c => cranberry + +*/ ?> ]]> </programlisting> @@ -112,11 +118,19 @@ </para> <para> After <function>each</function> has executed, the array cursor - will be left on the next element of the array, or on the last + will be left on the next element of the array, or past the last 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> + <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> <para> See also <function>key</function>, <function>list</function>, <function>current</function>, <function>reset</function>,
-- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php