Bug #70246 [Opn->Ver]: ArrayIterator skips next element when current element is unset in foreach.
| From: | cmb@php.net | Date: | Tue, 26 Jun 2018 16:57:46 +0000 |
| Subject: | Bug #70246 [Opn->Ver]: ArrayIterator skips next element when current element is unset in foreach. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215983@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70246&edit=1
ID: 70246
Updated by: cmb@php.net
Reported by: arjen at react dot com
Summary: ArrayIterator skips next element when current
element is unset in foreach.
-Status: Open
+Status: Verified
Type: Bug
Package: SPL related
Operating System: Linux
PHP Version: 7.0.0beta3
Block user comment: N
Private report: N
New Comment:
This regression has been introduced with commit 2b6a568[1]. The
first change in array_015.phpt already shows the problem.
[1] <http://git.php.net/?p=php-src.git;a=commit;h=2b6a568df119487f73abb43edd2ae82e83945356>
Previous Comments:
------------------------------------------------------------------------
[2018-06-26 08:48:42] requinix@php.net
Related To: Bug #76387
------------------------------------------------------------------------
[2018-06-26 08:44:26] derick@php.net
This bug is still being encountered in the wild, and it cost us considerable time to figure out what
was wrong: https://github.com/mongodb/mongo-php-library/pull/544/files#diff-d4c8c6dc8769324fc27cfdac19f05cafR88
------------------------------------------------------------------------
[2016-03-18 11:45:14] david at grudl dot com
Similar bug with stdClass and ArrayIterator: https://3v4l.org/2A1pf
------------------------------------------------------------------------
[2015-09-19 18:55:50] j dot tvr at centrum dot cz
This is probably not a bug. When you unset current element, the internal array pointer moves to the
next element. Therefore what you're doing is essentially equivalent to https://3v4l.org/rWQq8:
<?php
$ar = [1, 2, 3, 4, 5];
$it = new ArrayIterator($ar);
foreach ($it as $k => $v) {
if ($k === 0) $it->next(); // eq. to unset($it[0]);
echo "\$ar[$k] = $v;\n";
}
echo "\n";
print_r(iterator_to_array($it));
------------------------------------------------------------------------
[2015-08-18 11:56:39] arjen at react dot com
Expected and actual result are swapped, my mistake..
Expected result:
--------------
int(1)
int(2)
int(3)
int(4)
int(5)
---------
int(1)
int(3)
int(4)
int(5)
Actual result:
----------------
int(1)
int(2)
int(4)
int(5)
---------
int(1)
int(3)
int(4)
int(5)
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=70246
--
Edit this bug report at https://bugs.php.net/bug.php?id=70246&edit=1