Bug #76800 [Ver->Csd]: foreach inconsistent if array modified during loop

From: Date: Fri, 14 Sep 2018 07:33:58 +0000
Subject: Bug #76800 [Ver->Csd]: foreach inconsistent if array modified during loop
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-217041@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76800&edit=1 ID: 76800 Updated by: dmitry@php.net Reported by: leonix at motd dot ru Summary: foreach inconsistent if array modified during loop -Status: Verified +Status: Closed Type: Bug Package: Arrays related PHP Version: 7.2.9 Assigned To: dmitry Block user comment: N Private report: N New Comment: 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) Previous Comments: ------------------------------------------------------------------------ [2018-08-27 11:39:16] 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-08-27 09:16:59] cmb@php.net Behavior confirmed: <https://3v4l.org/8Oi0k> vs. <https://3v4l.org/MpCBe>. ------------------------------------------------------------------------ [2018-08-27 08:29:45] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76800&edit=1

« previous php.bugs (#217041) next »