ID: 23195 Updated by: [EMAIL PROTECTED] Reported By: jc at mega-bucks dot co dot jp -Status: Open +Status: Closed Bug Type: Documentation problem Operating System: Red Hat Linux 8.0 PHP Version: 4.3.2RC1 Assigned To: philip New Comment:
This behavior has been documented and will show up when the manual is next built, thanks for the report :) http://cvs.php.net/cvs.php/phpdoc/en/reference/array/functions/each.xml Also, added "explanation of pointers" to the TODO as currently no section in the manual clearly explains the topic of array pointers. Previous Comments: ------------------------------------------------------------------------ [2003-06-10 02:17:11] [EMAIL PROTECTED] pong ------------------------------------------------------------------------ [2003-06-10 02:05:00] jc at mega-bucks dot co dot jp Fine, it's a lack of documentation about some weird PHP behaviour :) (yes it *is* weird) Somebody please fix it. This bug keeps geeting closed and reopened and passed around. End the torment ^_~ ------------------------------------------------------------------------ [2003-06-10 01:58:52] [EMAIL PROTECTED] This isn't a bug in anything but docs.. ------------------------------------------------------------------------ [2003-06-10 00:53:04] [EMAIL PROTECTED] This makes no sense to me, why would assigning an array to another variable affect the original arrays pointer? Even the array it's assigned to keeps the original pointer: <?php $array = array('a','b','c'); print "1: " . current($array) . "\n"; // a print "2: " . next($array) . "\n"; // b $array2 = $array; print "3: " . current($array) . "\n"; // a print "4: " . current($array2). "\n"; // b ?> Either $array should also keep its pointer or $array2 should also have a reset pointer. I'd assume both would keep the current pointer, or maybe just $array2 would have a reset pointer :). Current behavior is certainly not documented, and are you guys sure this isn't a bug? ------------------------------------------------------------------------ [2003-04-25 00:10:48] jc at mega-bucks dot co dot jp Thanks for looking into this, but I cannot find anywhere in the documenation that states that assigning an array to a variable resets the array's pointer to the first element. Can you point me to such documentation? Furthermore if you are right, and I'm sure you are, then some of the documentation is wrong or confusing at best. Consider the following from the docs: http://www.php.net/manual/en/control-structures.foreach.php The following are also functionally identical: reset ($arr); while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br>\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>\n"; } But has you said, those are *not* functionally identical. Or from http://www.php.net/manual/en/function.each.php each() is typically used in conjunction with list() to traverse an array; for instance, $_POST: Example 2. Traversing $_POST with each() echo "Values submitted via POST method:<br />\n"; reset ($_POST); while (list ($key, $val) = each ($_POST)) { echo "$key => $val<br />\n"; } Reading the documentation on each() and on arrays the above two examples teach that using a while(list() = each()) is a correct way of traversing an array. Which it obviously is *not* if you plan on assigning the array you are traversing to a variable ... Either the each() function should be removed to force the use of the assignment-safe 'foreach()' or the documentation should mention that when assigning an array to a variable the current element-pointer is reset. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/23195 -- Edit this bug report at http://bugs.php.net/?id=23195&edit=1 -- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
