note 13476 added to control-structures.foreach.php

From: Date: Mon, 18 Jun 2001 16:59:03 +0000
Subject: note 13476 added to control-structures.foreach.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-14319@lists.php.net to get a copy of this message
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 } http://www.php.net/manual/en/control-structures.foreach.php

« previous php.notes (#14319) next »