note 33232 deleted from control-structures.foreach by sniper
| From: | sniper@php.net | Date: | Wed, 08 Oct 2003 04:04:27 +0000 |
| Subject: | note 33232 deleted from control-structures.foreach by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57999@lists.php.net to get a copy of this message | ||
Note Submitter: php@adamlewis.com
----
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);
}
}
}