#43220 [Opn]: foreach modifies array pointer

From: Date: Sun, 11 Nov 2007 18:47:21 +0000
Subject: #43220 [Opn]: foreach modifies array pointer
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-198@lists.php.net to get a copy of this message
ID: 43220 Updated by: jani@php.net Reported By: bugreporter at to dot mabomuja dot de Status: Open -Bug Type: *General Issues +Bug Type: Documentation problem Operating System: Linux/Windows PHP Version: 5.2.4 New Comment: Expected behaviour. Previous Comments: ------------------------------------------------------------------------ [2007-11-09 13:31:05] felipensp at gmail dot com The correct is: sizeof($t) - 1 ------------------------------------------------------------------------ [2007-11-09 02:51:00] bugreporter at to dot mabomuja dot de Actual Snaphots 5.3dev(2007-11-08-Snap) also has this bug and the code example is not working as i wrote above, sorry. ------------------------------------------------------------------------ [2007-11-09 02:45:29] bugreporter at to dot mabomuja dot de Description: ------------ In the code example below, when entering the for-loop, the arraypointer is at position 1 (the 2nd element). When you comment out the foreach-loop before OR insert the reset-line, output starts - as expected - at the first array element (posistion 0). This means "foreach" modifies the array pointer, although the documentation says "foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified" Testing with versions 4.4.7/5.2.3/5.3dev(2007-11-08-Snap) of Windows zip package and 4.4.7/5.2.1 on gentoo (5.2.3 not testet on linux) shows correct behavior. Reproduce code: --------------- $t=range('a','c'); foreach ($t AS $key => $profil){ $t[$key]="1"; } //reset($t); for ($i=0;$i<sizeof($t);$i++){ echo key($t) . "->".current($t)."\n"; next ($t); } print_r($t); Expected result: ---------------- version 5.2.3: 0->1 1->1 2->1 Array ( [0] => 1 [1] => 1 [2] => 1 ) Actual result: -------------- version 5.2.4: 1->1 2->1 -> Array ( [0] => 1 [1] => 1 [2] => 1 ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=43220&edit=1

« previous php.doc.bugs (#198) next »