Re: safe to delete elements of array in foreach



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.


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



Relevant Pages

  • Re: Dynamic Radio Buttons
    ... I execute a while loop that displays a group of questions the ... Any way to use PHP to accomplish my goal? ... Age[] and that IS the group as far as HTML is concerned. ... they are automagically turned into an array: ...
    (comp.lang.php)
  • Re: Dynamic Radio Buttons
    ... I execute a while loop that displays a group of questions the ... Any way to use PHP to accomplish my goal? ... Age[] and that IS the group as far as HTML is concerned. ... they are automagically turned into an array: ...
    (comp.lang.php)
  • Re: safe to delete elements of array in foreach
    ... 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, ...
    (comp.lang.php)
  • Re: Dynamic Radio Buttons
    ... I execute a while loop that displays a group of questions the ... Any way to use PHP to accomplish my goal? ... Age[] and that IS the group as far as HTML is concerned. ... If PHP receives for example the following 3 name/value pairs, they are automagically turned into an array: ...
    (comp.lang.php)
  • Re: safe to delete elements of array in foreach
    ... (normally this is not the case but not sure in php) ... 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, ...
    (comp.lang.php)