tags:

views:

54

answers:

3

i want to have a foreach loop where the initial array is changed inside the loop.

eg.

$array = array('red', 'blue');
foreach($array as $key => $value) {
    $array[] = 'white';
    echo $value . '<br />';
}

in this loop the loop will print out red and blue although i add another element inside the loop.

is there any way to change the initial array inside the loop so new elements will be added and the foreach will use the new array whatever is changed?

i need this kind of logic for a specific task:

i will have a if statement that search for a link. if that link exists, it is added to the array. the link content will be fetched to be examined if it contains another link. if so, this link is added, and the content will be fetched, so on so forth.. when no link is further founded, the foreach loop will exit

+2  A: 

What you need to do is move the assignment inside the for loop and check the length of the array every iteration.

$array = array('red', 'blue');
for($i = 0; $i < count($array); $i++)
{
   $value = $array[i];
   array_push($array, 'white');
   echo $value . '<br />';
}

Be careful, this will cause an infinite loop (white will be added to the end of the array at every loop).

MrValdez
+2  A: 

Maybe you should use some other way, like:

$ar = array('blue', 'red');
while ($a = array_pop($ar) {
     array_push($ar, 'white');
}

Or something like this...

Enrico Carlesso
how do u ensure that white only is added and printed out one time? cause your code gives an infinite loop
weng
I did not. As pointed as comment to the question, is up to the programmer to write smart code... Obviously as written by me it will run forever, it's just an example.
Enrico Carlesso
@noname you can use if .. else... before an array_push, and in_array to check if it is already on a duplicate array that just keeps "record" :)
DCC
+4  A: 

I don't think this is possible with a foreach loop, at least the way you wrote it : doesn't seem to just be the way foreach works ; quoting the manual page of foreach :

Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.


Edit : after thinking a bit about that note, it is actually possible, and here's the solution :

The note says "Unless the array is referenced" ; which means this portion of code should work :

$i = 0;
$array = array('red', 'blue');
foreach($array as $key => & $value) {
    $array[] = 'white';
    echo $value . '<br />';
    if ($i++ >= 5) {
        break;   // security measure to ensure non-endless loop
    }
}

Note the & before $value.

And it actually displays :

red
blue
white
white
white
white

Which means adding that & is actually the solution you were looking for, to modify the array from inside the foreach loop ;-)


Edit : and here is the solution I proposed before thinking about that note :

You could do that using a while loop, doing a bit more work "by hand" ; for instance :

$i = 0;

$array = array('red', 'blue');

$value = reset($array);
while ($value) {
    $array[] = 'white';
    echo $value . '<br />';
    if ($i++ >= 5) {
        break;   // security measure to ensure non-endless loop
    }
    $value = next($array);
}

Will get you this output :

red
blue
white
white
white
white
Pascal MARTIN
hmm why 4 white? i only want the added value to be iterated one time
weng
In the examples I posted, you get 4 `white` because of the security counter I used to prevent endless looping : the `break` will force the loop to end after 6 iterations *(The code you provided in your question was causing an endless loop, and I didn't want that in my examples)* ;;; in your application, up to you to find a solution that suits your need to avoid endless looping :-) *(judging from your question, you have ; even if you didn't include it as a part of your question)*
Pascal MARTIN
don't really understand why $value is referenced and not $array ...but it's good if it works... did you test this code ? Cheers.
DCC
Pascal MARTIN