Recently I experienced this weird problem:
while(list($key, $value) = each($array))
was not listing all array values, where replacing it with...
foreach($array as $key => $value)
...worked perfectly.
And, I'm curious now.. what is the difference between those two?
On each iteration, the value of the current element is assigned to $value . The second form will additionally assign the current element's key to the $key variable on each iteration. Note that foreach does not modify the internal array pointer, which is used by functions such as current() and key().
Declare the $items array outside the loop and use $items[] to add items to the array: $items = array(); foreach($group_membership as $username) { $items[] = $username; } print_r($items);
What are a key and value in an array? Keys are indexes and values are elements of an associative array. Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like a normal array and cannot be traversed using a normal for loop.
Had you previously traversed the array? each()
remembers its position in the array, so if you don't reset()
it you can miss items.
reset($array); while(list($key, $value) = each($array))
For what it's worth this method of array traversal is ancient and has been superseded by the more idiomatic foreach. I wouldn't use it unless you specifically want to take advantage of its one-item-at-a-time nature.
array each ( array &$array )
Return the current key and value pair from an array and advance the array cursor.
After
each()
has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to usereset()
if you want to traverse the array again usingeach
.
(Source: PHP Manual)
Well, one difference is that each()
will only work on arrays (well only work right). foreach
will work on any object that implements the traversable
interface (Which of course includes the built in array type).
There may be a micro-optimization in the foreach. Basically, foreach is equivilant to the following:
$array->rewind(); while ($array->valid()) { $key = $array->key(); $value = $array->current(); // Do your code here $array->next(); }
Whereas each
basically does the following:
$return = $array->valid() ? array($array->key(), $array->current()) : false; $array->next(); return $return;
So three lines are the same for both. They are both very similar. There may be some micro-optimizations in that each
doesn't need to worry about the traversable
interface... But that's going to be minor at best. But it's also going to be offset by doing the boolean cast and check in php code vs foreach
's compiled C... Not to mention that in your while/each
code, you're calling two language constructs and one function, whereas with foreach
it's a single language construct...
Not to mention that foreach
is MUCH more readable IMHO... So easier to read, and more flexible means that -to me- foreach
is the clear winner. (that's not to say that each
doesn't have its uses, but personally I've never needed it)...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With