note 33232 added to control-structures.foreach

From: Date: Thu, 19 Jun 2003 18:20:10 +0000
Subject: note 33232 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-50673@lists.php.net to get a copy of this message
I've found that although foreach makes a copy of the array it is working on, its wrangling of the internal array pointer creates unexpected results when a recursive function walks through a global array. If you want to make a recursive call (to yourself) from within a foreach and the foreach is looping on a global array, use the above mentioned array_keys method i.e. function recursive($param) { global $anArray; $anArrayKeys = array_keys($anArray); foreach ($anArrayKeys as $anArrayKey) { print $anArray[$anArrayKey].' '; if ($anArray[$anArrayKey] > $param) { //establish base case recursive($param+1); } } } ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33232 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33232&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33232&report=yes

« previous php.notes (#50673) next »