Bug #77014 [Nab]: Array traversal issues PHP 7+

From: Date: Thu, 02 Jul 2020 10:22:42 +0000
Subject: Bug #77014 [Nab]: Array traversal issues PHP 7+
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-227775@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77014&edit=1 ID: 77014 User updated by: miroslav at mishamosher dot com Reported by: miroslav at mishamosher dot com Summary: Array traversal issues PHP 7+ Status: Not a bug Type: Bug Package: Arrays related PHP Version: 7.2.11 Block user comment: N Private report: N New Comment: Email update Previous Comments: ------------------------------------------------------------------------ [2019-07-12 15:55:41] mishamosher at gmail dot com Thank you nikic for the information (: ------------------------------------------------------------------------ [2019-07-12 15:34:04] nikic@php.net The behavior is intended. Passing an array to a function does not change the position of the internal array pointer. I believe the PHP 5 behavior was some kind of artifact of key() accepting the array by reference and triggering a separation there. If you'd like to make sure that you are iterating the array from the start rather than from the previous position of the internal array pointer, you can perform an explicit call to reset(). (I'd recommend moving away from these functions entirely, if it is at all possible. If complex iteration patterns are involved, ArrayIterator may be an alternative.) ------------------------------------------------------------------------ [2019-07-12 15:16:02] mishamosher at gmail dot com Bump. Can anyone from the PHP team confirm if this behaviour is intended or if it is a bug? ------------------------------------------------------------------------ [2018-11-26 14:34:30] george dot banyard at gmail dot com I don't think this is a bug with the array traversal but more with the copying of the array state when the function is executed. If you reverse the order of execution as shown here: https://3v4l.org/l1bYA You get the same result in PHP5.x and 7.x Only difference that I know of is that before PHP7 foreach would use the internal pointer of the array which isn't necessary any more. So maybe there was a pointer reset mechanism for all loops which has been removed during the transition from 5 to 7. Also I do think the new behaviour in this case is Intended and wouldn't considered it a bug. ------------------------------------------------------------------------ [2018-10-15 15:09:30] miroslav at mishamosher dot com Description: ------------ There is a different behaviour between PHP 5.x and 7.x when traversing arrays. I can not find if this is intended, so I'm supposing its a bug. Test script: --------------- https://3v4l.org/YNJ18 Expected result: ---------------- I expect the same output on PHP 5.x and 7.x. Actual result: -------------- The outputs differ between PHP 5.x and 7.x. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77014&edit=1

« previous php.bugs (#227775) next »