Bug #53824 [Opn->Csd]: Array pointer unwanted reset
| From: | nikic@php.net | Date: | Tue, 07 Apr 2015 16:57:27 +0000 |
| Subject: | Bug #53824 [Opn->Csd]: Array pointer unwanted reset | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-191878@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=53824&edit=1
ID: 53824
Updated by: nikic@php.net
Reported by: php-guru at shark dot kom dot cz
Summary: Array pointer unwanted reset
-Status: Open
+Status: Closed
Type: Bug
Package: Arrays related
Operating System: Win XP, Win 7
PHP Version: 5.3.5
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
In PHP 7 foreach will no longer modify the internal array pointer.
Previous Comments:
------------------------------------------------------------------------
[2011-01-23 20:06:42] php-guru at shark dot kom dot cz
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 this bug report at https://bugs.php.net/bug.php?id=53824&edit=1