Posted by gosha bine on 05/21/07 08:36
On 20.05.2007 05:09 Jon Slaughter wrote:
>
> Well, your right. I assume that when it says copy it means a deep copy. If
> so then essentially its working on a different variable...
From my understanding, foreach doesn't make a copy of *entire array*,
it just copies *current value* into the loop variable (unless you
provided it by reference).
>
> If it was C/C++ then you would definitely have to worry about that sorta
> stuff and chances are would have to buffer the deletes or use some other
> method unless you can be absolutely sure its a deep copy.
>
> The more one dives into these sorta of things the more vague the solution
> is. It should be completely spelled out in the manual but its not ;/ These
> times of issues usually are a big deal and technically one can't assume
> anything but its working and I'm about 95% sure that I'm right in the way it
> works so I'm just going to assume that until otherwise. Obviously no one
> really seems to know the answer completely it seems ;/
>
I understand your concerns. Deleting elements in a loop might be
technically ok, but it just feels unsafe. Essentially what you're doing
is filtering and it'd better to write it as such:
$good = array();
foreach($ary as $elem)
if(my_check_func($elem)) $good[] = $elem;
or simply
$good = array_filter($ary, 'my_check_func');
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
[Back to original message]
|