Re: safe to delete elements of array in foreach
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Sat, 19 May 2007 22:35:01 -0400
ZeldorBlat wrote:
On May 18, 10:44 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:ZeldorBlat wrote:On May 18, 9:05 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:Yes, and in a case like this that change can break his code.ZeldorBlat wrote:I never said I disagreed with him -- in fact I, too, generally don'tOn May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@xxxxxxxxxxx>Sorry, I agree with Jon on this one.
wrote:
"ZeldorBlat" <zeldorb...@xxxxxxxxx> wrote in messageNo, the question is not to (sic) hard for me. But, as you've already
news:1179501018.125313.63600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@xxxxxxxxxxx>Um... cause I did... but that doesn't mean much. Just cause someone tries
wrote:
Is it safe to remove elements from an array that foreach is working on?Why don't you try it and see what happens?
(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
something doesn't prove that it will always work like that...
got any more bright ideas?
Or is the question to hard for you?
discovered, it isn't that difficult to test, either.
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
==================
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 :)
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.
I read that as just the opposite - changes in the copy do not affect the original array.
It doesn't say how often the copy is refreshed from the original array - or when changes in the original are reflected in the copy.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- safe to delete elements of array in foreach
- From: Jon Slaughter
- Re: safe to delete elements of array in foreach
- From: ZeldorBlat
- Re: safe to delete elements of array in foreach
- From: Jon Slaughter
- Re: safe to delete elements of array in foreach
- From: ZeldorBlat
- Re: safe to delete elements of array in foreach
- From: Jerry Stuckle
- Re: safe to delete elements of array in foreach
- From: ZeldorBlat
- Re: safe to delete elements of array in foreach
- From: Jerry Stuckle
- Re: safe to delete elements of array in foreach
- From: ZeldorBlat
- safe to delete elements of array in foreach
- Prev by Date: Re: is sql just for hobbiest?
- Next by Date: Re: safe to delete elements of array in foreach
- Previous by thread: Re: safe to delete elements of array in foreach
- Next by thread: Re: safe to delete elements of array in foreach
- Index(es):
Relevant Pages
|