#23624 [Ver->Opn]: foreach internal array pointer issue
ID: 23624
Updated by: philip@php.net
Reported By: nicolas at van-lancker dot be
-Status: Verified
+Status: Open
Bug Type: Scripting Engine problem
Operating System: any
PHP Version: 4.3.2-RC3-dev
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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
Thread (6 messages)