Bug #62123 [NEW]: Foreach changes the internal pointer of the array strangely

From: Date: Wed, 23 May 2012 13:23:38 +0000
Subject: Bug #62123 [NEW]: Foreach changes the internal pointer of the array strangely
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-170721@lists.php.net to get a copy of this message
From:             
Operating system: Linux
PHP version:      5.4.3
Package:          Arrays related
Bug Type:         Bug
Bug description:Foreach changes the internal pointer of the array strangely

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 bug report at https://bugs.php.net/bug.php?id=62123&edit=1
-- 
Try a snapshot (PHP 5.4):            https://bugs.php.net/fix.php?id=62123&r=trysnapshot54
Try a snapshot (PHP 5.3):            https://bugs.php.net/fix.php?id=62123&r=trysnapshot53
Try a snapshot (trunk):              https://bugs.php.net/fix.php?id=62123&r=trysnapshottrunk
Fixed in SVN:                        https://bugs.php.net/fix.php?id=62123&r=fixed
Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=62123&r=needdocs
Fixed in release:                    https://bugs.php.net/fix.php?id=62123&r=alreadyfixed
Need backtrace:                      https://bugs.php.net/fix.php?id=62123&r=needtrace
Need Reproduce Script:               https://bugs.php.net/fix.php?id=62123&r=needscript
Try newer version:                   https://bugs.php.net/fix.php?id=62123&r=oldversion
Not developer issue:                 https://bugs.php.net/fix.php?id=62123&r=support
Expected behavior:                   https://bugs.php.net/fix.php?id=62123&r=notwrong
Not enough info:                     https://bugs.php.net/fix.php?id=62123&r=notenoughinfo
Submitted twice:                     https://bugs.php.net/fix.php?id=62123&r=submittedtwice
register_globals:                    https://bugs.php.net/fix.php?id=62123&r=globals
PHP 4 support discontinued:          https://bugs.php.net/fix.php?id=62123&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=62123&r=dst
IIS Stability:                       https://bugs.php.net/fix.php?id=62123&r=isapi
Install GNU Sed:                     https://bugs.php.net/fix.php?id=62123&r=gnused
Floating point limitations:          https://bugs.php.net/fix.php?id=62123&r=float
No Zend Extensions:                  https://bugs.php.net/fix.php?id=62123&r=nozend
MySQL Configuration Error:           https://bugs.php.net/fix.php?id=62123&r=mysqlcfg



Thread (4 messages)

« previous php.bugs (#170721) next »