Re: foreach / unset

From: Date: Fri, 28 Oct 2005 13:02:01 +0000
Subject: Re: foreach / unset
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-224773@lists.php.net to get a copy of this message
Richard Lynch wrote:
Somewhere in the manual (damned if I can find it now) it says (or used to say) that you can or can't safely do this: while (list($k, $v) = each($array)){ if (...) unset($array[$k]); } I don't even remember if it's safe or not, but I swear I saw it not that long ago... Anyway, can you do *this* safely as a DOCUMENTED FEATURE: foreach($array as $k => $v){ if (...) unset($array[$k]); } I'm sure I could test it and maybe find out if "it works" but is it documented behaviour I can rely on? I'm sure not finding this in the manual now that I go looking for it, though I know I saw it there before.
I would *think* (just my opinion without much thought on a Friday morning) that this would/could be unsafe _if_ it was a for loop on a numerical indexed array. Of course, my thinking may change after my first bottle of Dew. ;)
PS I'm being dragged kicking and screaming into using this new-fangled 'foreach' thing instead of while/list/each, and I don't really care for it so far. :-)
For the longest time, I hated foreach. Looked at other people's code who used it, and just wanted to strangle them. However, I was sorta forced into it about 8 months ago, and now I'm in the camp of, "Damn, I really like this". -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 john@kegworks.com

« previous php.general (#224773) next »