Re: safe to delete elements of array in foreach



Jon Slaughter wrote:
"Jerry Stuckle" <jstucklex@xxxxxxxxxxxxx> wrote in message news:lcWdnQnjjd1qKdLbnZ2dnUVZ_oLinZ2d@xxxxxxxxxxxxxx
Schraalhans Keukenmeester wrote:
At Fri, 18 May 2007 21:05:36 -0400, Jerry Stuckle let his monkeys type:

ZeldorBlat wrote:
On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@xxxxxxxxxxx>
wrote:
"ZeldorBlat" <zeldorb...@xxxxxxxxx> wrote in message

news:1179501018.125313.63600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@xxxxxxxxxxx>
wrote:
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?
Thanks,
Jon
Why don't you try it and see what happens?
Um... cause I did... but that doesn't mean much. Just cause someone tries
something doesn't prove that it will always work like that...

got any more bright ideas?

Or is the question to hard for you?
No, the question is not to (sic) hard for me. But, as you've already
discovered, it isn't that difficult to test, either.

Sorry, I agree with Jon on this one.

I make it a habit not to delete entries in a foreach() loop. Rather, I build an array of keys I want to delete, and after the loop ends, delete the entries from my delete array.

I don't know whether an operation like this is guaranteed to work in PHP - I've never seen it documented, so I suspect not. And just because it works in one release under a certain set of conditions is not a guarantee it will work on another release or under different conditions.
I agree 100% with you not to rely on undocumented 'features'.
Just out of curiosity I took this one step further and discovered the
following (again, can't rely on this to hold true unless it's documented
somewhere, well hidden):

<?PHP
$array=array('john','james','delilah','mary');
foreach ($array as $key=>$value) {
echo "$key => $value".NEWLINE;
if ($value == 'delilah') {
$array[$key]='samson';
} elseif ($value=='james') {
unset($array[$key]);
}
}
echo NEWLINE;
foreach ($array as $key=>$value) {
echo "$key => $value".NEWLINE;
}
?>
0 => john
1 => james
2 => delilah
3 => mary

0 => john
2 => samson
3 => mary

The foreach loop operates on a copy of the array
The original array remains in scope
Current($array) points to the first element throughout the loop

I'd think setting elements to NULL directly in the array isn't any better,
you're still relying on the same 'feature'.

Sh
Yes, foreach() works on a copy. But while the manual indicates changes to the copy don't affect the original - they say nothing about when changes to the original will affect the copy.


since its a copy the original should effect in any way the copy?

I could only see this if the copy is not a deep copy.... but in php all copies are deep?



There is nothing indicating when the copy is updated from the original. It might be only at the beginning, everything through the loop or anything else.

It's not documented - so you can't depend on the operation.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: safe to delete elements of array in foreach
    ... I agree with Jon on this one. ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, delete ... I don't know whether an operation like this is guaranteed to work in PHP ...
    (comp.lang.php)
  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)