note 13476 deleted from control-structures.foreach by sniper
| From: | sniper@php.net | Date: | Tue, 10 Jun 2003 22:54:40 +0000 |
| Subject: | note 13476 deleted from control-structures.foreach by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-50140@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster@chikanNE_PAS_DE_SPAM.co.uk
----
I noticed problems while looping through an associative array using foreach. In the main loop, I ran
a function which needed to loop through the *same* array. Both loops were done using foreach. The
loop in the function was messing up the main loop. To circumvent this, I looped through a copy of
the array in the function.
Illustration:
//Main loop
foreach ($sections as $section=>$sectioninfo) {
//do various things
x = AFunction();
}
function AFunction() {
global $sections;
foreach ($sections as $section=>$sectioninfo) {
//do various things
}
return $something
}
The loop in the function messes up the main loop. IE, not all elements are visited. So, I made this
modification to the function:
function ANewFunction() {
global $sections;
$temp = $sections;
foreach ($temp as $section=>$sectioninfo) {
//do various things
}
return $something
}