RE: modifying within foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 foreach ($results as $key => $item) 
 {
    if ($item == 'foo') unset($results[$key]);
 }

> -----Original Message-----
> From: Andres Gonzalez [mailto:andres@xxxxxxxxxxxxxxx] 
> Sent: Tuesday, June 23, 2009 11:27 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  modifying within foreach
> 
> In the following example:
> 
> foreach ($results as $key => $item)  {
> 
>     //bla bla bla -- unset some of the $items
> 
> }
> 
> I want to modify $results within the foreach. In other words,
> during a given pass of this iteration, I want to delete some
> of the items based on particular conditions. Then on the next
> pass thru the foreach, I want $results to be the newer, modified
> array.
> 
> This does not seem to work. It appears that the foreach statement
> is implemented such that $results is read into memory at the start
> so that any modifications I make to it during a given pass, 
> are ignored
> on the next pass. Is this true?
> 
> If so, is there a way that I can tell the foreach statement 
> to re-read the
> array $results?  Or am I just going against the grain here?
> 
> -Andres
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux