Posted by Rami Elomaa on 05/19/07 04:42
Jon Slaughter kirjoitti:
> Is it safe to remove elements from an array that foreach is working on?
> (normally this is not the case but not sure in php) If so is there an
> efficient way to handle it? (I could add the indexes to a temp array and
> delete afterwards if necessary but since I'm actually working in a nested
> situation this could get a little messy. I guess I could set there values to
> null and remove them afterwards?
$a = range(0,10);
foreach($a as $key => $val)
if($val%2)
unset($a[$key]);
I've done something like this and have never had any problems. I don't
see the threats in this kind of thing, can you explain me what you think
that might be unsafe in this method?
--
Rami.Elomaa@gmail.com
"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Navigation:
[Reply to this message]
|