php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Your comment was added to the bug successfully.
Bug #76800 foreach inconsistent if array modified during loop
Submitted: 2018-08-27 08:29 UTC Modified: 2022-08-06 07:50 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: leonix at motd dot ru Assigned: dmitry (profile)
Status: Closed Package: Arrays related
PHP Version: 7.2.9 OS:
Private report: No CVE-ID: None
 [2018-08-27 08:29 UTC] leonix at motd dot ru
Description:
------------
Hey. I encountered a weird inconsistency in foreach() behavior over plain arrays. See the code below. In certain specific cases loop will skip second iteration with no apparent reason.

It reproduces (at least) on 7.2.7-7.2.9 on both windows and linux, but NOT on 7.1.21 or below.

Test script:
---------------
$arr = [1 => 1, 3 => 3];           // [1 => 1, 2 => 3] will print both keys
foreach($arr as $key => &$val) {   // without & will print both keys
    echo "See key {$key}\n";
    $arr[0] = 0;                   // without this line will print both keys
    unset($arr[0]);
}


Expected result:
----------------
See key 1
See key 3


Actual result:
--------------
See key 1


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-27 09:16 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2018-08-27 09:16 UTC] cmb@php.net
Behavior confirmed: <https://3v4l.org/8Oi0k> vs. <https://3v4l.org/MpCBe>.
 [2018-08-27 11:39 UTC] cmb@php.net
-Assigned To: +Assigned To: dmitry
 [2018-08-27 11:39 UTC] cmb@php.net
The regression seems to have been introduced with commit
31a2198[1].  Dmitry, could you please have a look?

[1] <http://git.php.net/?p=php-src.git;a=commit;h=31a2198>
 [2018-09-14 07:33 UTC] dmitry@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=3bc4a63fc27768c665fcbc101db4e579c761833b
Log: Fixed bug #76800 (foreach inconsistent if array modified during loop)
 [2018-09-14 07:33 UTC] dmitry@php.net
-Status: Verified +Status: Closed
 [2022-08-06 07:44 UTC] rkciviljungle at gmail dot com
Open VS Code.
Open the “Extensions” sidebar (you can use “Ctrl+Shift+X”).
Click on the ellipsis icon in the top right corner of the menu.
Select “Install from VSIX…”
VS Code will open a document browser. ...
VS Code will now begin the installation process of the extension.
 [2022-08-06 07:50 UTC] rathod261190 at gmail dot com
Typically, a mix is about 10 to 15 percent cement, 60 to 75 percent aggregate and 15 to 20 percent water. Entrained air in many concrete mixes may also take up another 5 to 8 percent.
 (https://civiljungle.com/concrete-mix-ratio/)github.com
 [2022-08-06 07:50 UTC] requinix@php.net
-Block user comment: No +Block user comment: Yes
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC