Re: safe to delete elements of array in foreach



On May 18, 9:05 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
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.
jstuck...@xxxxxxxxxxxxx
==================

I never said I disagreed with him -- in fact I, too, generally don't
delete elements inside a foreach. However, I will say that when I
have done it things seem to work as expected. I guess it all comes
down to whether or not the array's internal pointer is modified when
you unset the element it's pointing to (I suspect it isn't).

I see a lot of questions in these newsgroups that look something like,
"What happens if I do X?" or "In PHP is this code valid?" The point I
was trying to make (and apparently Jon took offense to it) was that
it's easy enough to just try it and see what happens. Software is
just that: soft. It can be changed easily enough :)


.



Relevant Pages

  • Re: safe to delete elements of array in foreach
    ... 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, ... 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. ...
    (comp.lang.php)
  • Re: [PHP] What search algorithm does in_array() use?
    ... On Mon, January 29, 2007 11:20 am, Ken Dozier wrote: ... check sequentially each element in the array? ... Since there is no guarantee that the elements are in any particular ... don't do any search in PHP at all. ...
    (php.general)
  • Re: Need help on PHP for MPE/ix
    ... Here is the syntax of the dbupdate intrinsic in PHP: ... assoc array of item values) ... Please let me know if you have any more suggesstions to solve this problem. ... Pavan Kumar Rati wrote: ...
    (comp.sys.hp.mpe)
  • Re: Using a RegEx as a "variable" WITHIN an array?
    ... believe the PHP one is also based upon. ... combination of 1 or more numerics in this instance would give you your ... add this to your array - you could then do a pattern match on the array ... > Dim selCriteria as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Need help on PHP for MPE/ix
    ... If one item is an array, it must be declared as such in the list ... So, please show us the declaration of the record buffer, and its ... Of Pavan Kumar Rati ... Need help on PHP for MPE/ix ...
    (comp.sys.hp.mpe)