Re: Question on foreach()?

From: Date: Fri, 14 Jul 2000 16:25:53 +0000
Subject: Re: Question on foreach()?
References: 1 2  Groups: php.dev 
Request: Send a blank email to php-dev+get-24554@lists.php.net to get a copy of this message
Hi Joey! On Fri, 14 Jul 2000, Joey Smith wrote: > I should have been more clear, I guess. > > I am fully aware of the > foreach($array as $key => $value) > structure. What I am asking is WHY the shorter version deals with > values instead of keys. This was more a ? for one of the core guys, trying > to understand conceptually WHY something was done. but what for each' suggests for an array like `array ('a','b','c')''? I would read for each element, do something', not for each position do something'. Plus, it saves a lookup for each element, imagine foreach walking over keys: foreach ($arr as $i) { do_something_with($arr[$i]); } ugly, you have to access the array twice, first in the foreach loop, and second when using the array index. why not good old `while (list ($k,$v) = each ($arr)){}' instead for this ? -- teodor

« previous php.dev (#24554) next »