Home » Php » php – while(list($key, $value) = each($array)) vs. foreach($array as $key => $value)?

php – while(list($key, $value) = each($array)) vs. foreach($array as $key => $value)?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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 use reset() if you want to traverse the array again using each.

(Source: PHP Manual)

Answer:

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)…

Answer:

Warning! Foreach creates a copy of the array so you cannot modify it while foreach is iterating over it. each() still has a purpose and can be very useful if you are doing live edits to an array while looping over it’s elements and indexes.

// Foreach creates a copy
$array = [
  "foo" => ['bar', 'baz'],
  "bar" => ['foo'],
  "baz" => ['bar'],
  "batz" => ['end']
];

// while(list($i, $value) = each($array)) { // Try this next
foreach($array as $i => $value) {
  print $i . "\n";
  foreach($value as $index) {
    unset($array[$index]);
  }
}

print_r($array); // array('baz' => ['end'])

Both foreach and while will finish their loops and the array “$array” will be changed. However, the foreach loop didn’t change while it was looping – so it still iterated over every element even though we had deleted them.

Update: This answer is not a mistake.

I thought this answer was pretty straight forward but it appears the majority of users here aren’t able to appreciate the specific details I mention here.

Developers that have built applications using libdom (like removing elements) or other intensive map/list/dict filtering can attest to the importance of what I said here.

If you do not understand this answer it will bite you some day.

Answer:

If you passed each an object to iterate over, the PHP manual warns that it may have unexpected results.

What exactly is in $array