From:
Operating system: Win XP, Win 7
PHP version: 5.3.5
Package: Arrays related
Bug Type: Bug
Bug description:Array pointer unwanted reset
Description:
------------
When looping by foreach on 2D array instead of it's array keys inner
arrays' internal pointer is reset.
Test script:
---------------
<?php
$outer = array(
'A' => array(0 => '1st', 1 => '2nd', 3 => '3rd'),
'B' => array( 1 => '2nd', 2 => 'still 2nd', 4 => '3rd', 5 =>
'still 3rd'),
);
$syncMarks = array(1, 3);
do {
$next = FALSE;
$current = array();
$syncMark = current($syncMarks);
//foreach (array_keys($outer) as $key) {
foreach ($outer as $key => $inner) {
$offset = key($outer[$key]);
if (isset($offset)) {
$next = TRUE;
if ($syncMark === FALSE || $offset < $syncMark) {
$current[$key] = current($outer[$key]);
next($outer[$key]);
}
}
}
var_dump($current);
} while (empty($current) && next($syncMarks) || $next);
Expected result:
----------------
array(1) {
["A"]=>
string(3) "1st"
}
array(0) {
}
array(2) {
["A"]=>
string(3) "2nd"
["B"]=>
string(3) "2nd"
}
array(1) {
["B"]=>
string(9) "still 2nd"
}
array(0) {
}
array(2) {
["A"]=>
string(3) "3rd"
["B"]=>
string(3) "3rd"
}
array(1) {
["B"]=>
string(9) "still 3rd"
}
array(0) {
}
Actual result:
--------------
array(1) {
["A"]=>
string(3) "1st"
}
array(0) {
}
array(2) {
["A"]=>
string(3) "2nd"
["B"]=>
string(3) "2nd"
}
array(1) {
["B"]=>
string(9) "still 2nd"
}
array(0) {
}
array(2) {
["A"]=>
string(3) "3rd"
["B"]=>
string(3) "3rd"
}
array(2) {
["A"]=>
string(3) "1st" // <-- This is unwanted result
["B"]=>
string(9) "still 3rd"
...
--
Edit bug report at http://bugs.php.net/bug.php?id=53824&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=53824&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=53824&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=53824&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=53824&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=53824&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=53824&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=53824&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=53824&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=53824&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=53824&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=53824&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=53824&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=53824&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=53824&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53824&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53824&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=53824&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=53824&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=53824&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=53824&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=53824&r=mysqlcfg