Bug #62123 [Com]: Foreach changes the internal pointer of the array strangely

From: Date: Wed, 23 May 2012 23:59:54 +0000
Subject: Bug #62123 [Com]: Foreach changes the internal pointer of the array strangely
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-170742@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62123&edit=1 ID: 62123 Comment by: a dot protaskin at gmail dot com Reported by: a dot protaskin at gmail dot com Summary: Foreach changes the internal pointer of the array strangely Status: Open Type: Bug Package: Arrays related Operating System: Linux PHP Version: 5.4.3 Block user comment: N Private report: N New Comment: Thanks for your answer, nikic. I understood this behaviour, this is not the bug. Previous Comments: ------------------------------------------------------------------------ [2012-05-23 21:54:57] nikic@php.net See http://stackoverflow.com/a/8263425/385378 for an explanation. ------------------------------------------------------------------------ [2012-05-23 13:23:38] a dot protaskin at gmail dot com Description: ------------ Traversing the array changes once the internal pointer by 1. Traversing the array by link changes the internal pointer just as with each(). Test script: --------------- <?php $array = [0, 1, 2]; while (each($array)) { var_dump(key($array)); } reset($array); echo "----\n"; foreach ($array as $elem) { var_dump(key($array)); } reset($array); echo "----\n"; $array2 = &$array; foreach ($array2 as $elem) { var_dump(key($array)); } Expected result: ---------------- int(1) int(2) NULL ---- int(1) int(2) NULL ---- int(1) int(2) NULL Actual result: -------------- int(1) int(2) NULL ---- int(1) int(1) int(1) ---- int(1) int(2) NULL ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62123&edit=1

« previous php.bugs (#170742) next »