Edit report at https://bugs.php.net/bug.php?id=62123&edit=1

 ID:                 62123
 Comment by:         a dot protaskin at gmail dot com
 Reported by:        a dot protaskin at gmail dot com
 Summary:            Foreach changes the internal pointer of the array
                     strangely
 Status:             Open
 Type:               Bug
 Package:            Arrays related
 Operating System:   Linux
 PHP Version:        5.4.3
 Block user comment: N
 Private report:     N

 New Comment:

Thanks for your answer, nikic. I understood this behaviour, this is not the bug.


Previous Comments:
------------------------------------------------------------------------
[2012-05-23 21:54:57] ni...@php.net

See http://stackoverflow.com/a/8263425/385378 for an explanation.

------------------------------------------------------------------------
[2012-05-23 13:23:38] a dot protaskin at gmail dot com

Description:
------------
Traversing the array changes once the internal pointer by 1.
Traversing the array by link changes the internal pointer just as with each().

Test script:
---------------
<?php
$array = [0, 1, 2];
while (each($array)) {
    var_dump(key($array));
}

reset($array);
echo "----\n";

foreach ($array as $elem) {
    var_dump(key($array));
}

reset($array);
echo "----\n";

$array2 = &$array;
foreach ($array2 as $elem) {
    var_dump(key($array));
}

Expected result:
----------------
int(1)
int(2)
NULL
----
int(1)
int(2)
NULL
----
int(1)
int(2)
NULL

Actual result:
--------------
int(1)
int(2)
NULL
----
int(1)
int(1)
int(1)
----
int(1)
int(2)
NULL


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62123&edit=1

Reply via email to