Re: safe to delete elements of array in foreach



Jon Slaughter wrote:
"Jerry Stuckle" <jstucklex@xxxxxxxxxxxxx> wrote in message news:lcWdnQ7jjd3FKdLbnZ2dnUVZ_oLinZ2d@xxxxxxxxxxxxxx
Jon Slaughter wrote:
"ZeldorBlat" <zeldorblat@xxxxxxxxx> wrote in message news:1179544257.218638.219550@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On May 18, 10:44 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
ZeldorBlat wrote:
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 :)
Yes, and in a case like this that change can break his code.

As I said - I've never seen it documented that this is valid. Maybe it
is and I missed it; I really don't know.

But this isn't the same as a lot of other "try it and find out"
questions. In this case it's a known problem in other programming
languages, and if it is documented that this should or should not work,
no one has pointed anyone to it.

And until I see something from the PHP developers saying it is OK, I
wouldn't do it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================
This suggests that it's safe (from <http://www.php.net/foreach>):

"Unless the array is referenced, foreach operates on a copy of the
specified array and not the array itself. Therefore, the array pointer
is not modified as with the each() construct, and changes to the array
element returned are not reflected in the original array."

So unsetting a value in the original array should not affect the copy
that foreach is working on.

Your right. I saw this after the fact after I did try it out and compared it with a for loop. The foreach worked while the for loop didn't. Then I went to the manual and saw that it worked on a copy.

So in this case it should work just fine. The for looped version actually doesn't work and does give the problem I suspected. (thought I sent a post a bout it though).

So unless they go change how foreach works in a newer version I think it will be safe. I'm still trying to get used to php as it does a lot of things differently than I'm used to.

The issue with "trying it out" is that in things like this you can't be sure since its a implementation artifact... The only way to know is to know that foreach is working on a copy. Of course maybe one could realize that by doing a few examples but when usually one doesn't want to rely on what they think is going on in this sorta situation and needs to know the facts.

Of course now I know its completely safe which was my original question ;) It came from the manual which is where I should have looked in the fist place(I did but I guess I skipped over that part ;/

Thanks,
Jon
Jon,

I disagree it's "completely safe". All this is saying is that the changes to the copy will not affect the original. It says nothing about what changes to the original will affect.



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...

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 ;/



Yes, it does say a copy. But it says nothing about then the copy is refreshed from the original.

Typically manuals indicate what is valid - but not what is not valid. There are too may possibilities to cover.

In 40 years of programming, I've learned never to assume something. I've been burned too many times. And it makes you look very bad to your clients.


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



Relevant Pages

  • 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
    ... (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)
  • 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 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: 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)