#23624 [Opn->Ver]: foreach internal array pointer issue

From: Date: Wed, 14 May 2003 15:36:52 +0000
Subject: #23624 [Opn->Ver]: foreach internal array pointer issue
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-39632@lists.php.net to get a copy of this message
ID: 23624 Updated by: sniper@php.net Reported By: nicolas at van-lancker dot be -Status: Open +Status: Verified Bug Type: Scripting Engine problem Operating System: any PHP Version: 4.3.2-RC3-dev New Comment: This is more common problem: <?php $arr = array("one", "two", "three"); var_dump(current($arr)); foreach ($arr as $v); var_dump(current($arr)); reset($arr); var_dump(current($arr)); while(each($arr)); var_dump(current($arr)); ?> And output is: string(3) "one" bool(false) string(3) "one" bool(false) So, as you can see, it's not just foreach(), each() also does the same, ie. sets current key to null. IMO, current documentation is correct about it. (the key should be set to the last key) The question is if it should be fixed, as it's propably been around there since the beginning..but I leave that decision to Zeev / Andi. For the moment, leave this as 'Verified'. Previous Comments: ------------------------------------------------------------------------ [2003-05-14 10:28:57] philip@php.net Here's the original report on this [related] issue: http://bugs.php.net/bug.php?id=8353 foreach affects the pointer and moves it "past" the end. I assume that if your "after the each" printed "three" that we would have an endless loop printing "three" infinity times... For the record I don't think foreach() should touch the pointer at all but it does so this simply looks like a documentation problem. Rather than say "end" it should say something like "moves past the end". Hopefully a php-dev person can provide proper terminology for this behavior in which case it will be documented. ------------------------------------------------------------------------ [2003-05-14 03:50:22] nicolas at van-lancker dot be According to the documentation : Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the each() construct and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array. Given the code : <?php $arr = array("one", "two", "three"); echo "current : " . current($arr) . "<br>"; foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>"; } echo "after for each : " . current($arr) . "<br>"; ?> Code result : current : one Key: 0; Value: one Key: 1; Value: two Key: 2; Value: three after for each : It seems that the internal array pointer indeed has been moved while iterating. But I would expect that it should be a the end of the original array, returning me a value "three" instead of nothing. When calling prev($arr); it still doesn't return anything. Maybe the interal array pointer has gone beserk? This might be a wrong reflexion by me and not be a bug. But it find it a bit confusing... ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=23624&edit=1

« previous php.bugs (#39632) next »