note 28464 added to control-structures.foreach

From: Date: Tue, 14 Jan 2003 08:46:29 +0000
Subject: note 28464 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-42109@lists.php.net to get a copy of this message
Another "nested foreach" effect : <? global $arr; $arr = array('a', 'b', 'c', 'd'); function f2() { global $arr; foreach ($arr as $k=>$v) { echo "$k=>$v<br>\n"; } } function f1() { global $arr; foreach ($arr as $k=>$v) { f2(); } } f1(); ?> a global array is foreach()ed by two inner function. The internal pointer of the array is reset on each foreach(), and because there is only one pointer in the global array, it doesn't behava as expected. Workaround : use list() and for() construct. -- http://www.php.net/manual/en/control-structures.foreach.php http://master.php.net/manage/user-notes.php?action=edit+28464 http://master.php.net/manage/user-notes.php?action=delete+28464 http://master.php.net/manage/user-notes.php?action=reject+28464

« previous php.notes (#42109) next »