note 54290 rejected from control-structures.foreach by betz

From: Date: Fri, 08 Jul 2005 08:03:52 +0000
Subject: note 54290 rejected from control-structures.foreach by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-91644@lists.php.net to get a copy of this message
Note Submitter: rokas3 at gmail dot com ---- One notice that foreach has strange behaviour with copying array. It sometimes doubles one array item. For example: <?php $array = array( 0 => array("one" => "x", "two" => "y"), 1 => array("one" => "z", "two" => "a"), 2 => array("one" => "b", "two" => "c") ) foreach ($array as $item) { echo $item["one"]." - ".$item["two"]."<br />"; } /* x - y z - a z - a b - c */ This can be avoided with reference: foreach ($array as &$item) { echo $item["one"]." - ".$item["two"]."<br />"; } /* x - y z - a b - c */

« previous php.notes (#91644) next »