You are here: Re: [PHP] foreach / unset « PHP « IT news, forums, messages
Re: [PHP] foreach / unset

Posted by "Richard Lynch" on 10/09/05 11:30

On Fri, October 28, 2005 8:47 am, Jochem Maas wrote:
> John Nichel wrote:
>> Richard Lynch wrote:
>>
>>> Somewhere in the manual (damned if I can find it now) it says (or
>>> used
>>> to say) that you can or can't safely do this:
>>>
>>> while (list($k, $v) = each($array)){
>>> if (...) unset($array[$k]);
>>> }
>>>
>>> I don't even remember if it's safe or not, but I swear I saw it not
>>> that long ago...
>>>
>>> Anyway, can you do *this* safely as a DOCUMENTED FEATURE:
>>>
>>> foreach($array as $k => $v){
>>> if (...) unset($array[$k]);
>>> }
>>>
>>> I'm sure I could test it and maybe find out if "it works" but is it
>>> documented behaviour I can rely on? I'm sure not finding this in
>>> the
>>> manual now that I go looking for it, though I know I saw it there
>>> before.

I would think it would be just fine *EXCEPT* that the "internal
pointer" of the original array is being incremented in parallel with
the copied array key/values, so it ends up at the "end" after the
"foreach"

So, unless it's a DOCUMENTED FEATURE rather than an implementation
detail, I don't really want to rely on unset($original[$k]) "working"
because who knows what might change in PHP6 with the "internal
pointer" and unsetting the current entry it points to, or, rather, the
entry it previously pointed to.

I know for sure that while/list/each and an integer-indexed array and
(unset($array[$k + 1])) is a big mistake :-)

>> I would *think* (just my opinion without much thought on a Friday
>> morning) that this would/could be unsafe _if_ it was a for loop on a
>> numerical indexed array. Of course, my thinking may change after my
>> first bottle of Dew. ;)

I think it's always been okay to unset the *CURRENT* element, but if
you started dinking with other elements, in some sort of poor-man's
relational algorithm, you'd be in trouble.

foreach doesn't DOCUMENT unset-ing either way...

>>> PS
>>> I'm being dragged kicking and screaming into using this new-fangled
>>> 'foreach' thing instead of while/list/each, and I don't really care
>>> for it so far. :-)
>
> new-fangled is putting the boat out a bit thought :-) it's been around
> since 4.0,
> I guess your looking forward to being able to use foreach to iterate
> over php5 object ;-)

For me, who started in PHP 3.0 RC 2, PHP 4.0 *is* new-fangled. PHP 5
is just *more* new-fangled.

I suppose by PHP6 I'll be just plain obsolete and have to start
looking for a new language. :-v

>> For the longest time, I hated foreach. Looked at other people's
>> code
>> who used it, and just wanted to strangle them. However, I was sorta
>> forced into it about 8 months ago, and now I'm in the camp of,
>> "Damn, I
>> really like this".

I don't like it precisely because it's not documented "what if"...

while/list/each actually HAD documentation at one point that it was
kosher to unset() the current element, but no other.

Though that also seems to have gone missing from the manual, possibly
in the Great Purge of not so long ago (from a PHP3.0rc2 persepctive of
time).

I also just don't care for the clever crammed syntax and the "as"
keyword. It just doesn't go through my brain easily, no matter how
many times I see it.

I guess I'm just so used to assignments being left <- right, where
foreach has it right -> left for what's being assigned to what...

How about:

eachfrom([$k => ] $v = $array){
}

Now, see, that would make perfect sense to me...

[shrug]

DSFDF

--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация