42

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?

2
  • 2
    Note: foreach() is great for 96% of all use cases. However, sometimes you have to alter the array itself during iteration and that is where the power of each() comes in since foreach copies the array internally. See my answer below. Anyone that has worked with libxml can attest to the dangers of blindly using foreach for every situation. Commented Aug 31, 2015 at 19:08
  • 5
    Side note that each has been deprecated in PHP 7.2. Commented May 4, 2017 at 15:42

4 Answers 4

59

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Good comparison. I found that while each is a good way to add elements to $array from within the while block if needed.
Very interesting comparison ! What about the performance difference between each method, are they practically the same ?
14

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

4 Comments

It's not exactly the same because foreach will behave as if it's operating in a copy of the array. For instance, if you change an element of the original array during iteration, foreach will still give you the original one. This behavior only changes if you start using references (e.g. foreach ($array as &$v) {}) because in that case you're signaling your intention to change the array. See codepad.viper-7.com/bGaIqc
There's another problem with your answer. Arrays don't implement the Traversable interface; they're not even objects (try to build an IteratorIterator with an array, which has Traversable type hint). For foreach purposes, they behave like one, but in some cases, you have to use ArrayIterator to convert it to a traversable object.
The internal Array object does implement Traversable. You cannot use it in place of a iterator because it does not implement Iterator or IteratorAggregate... But internally, it does function the same...
I'm afraid you're mistaken. Arrays are not objects. There is no Array class as you can easily see by checking the return value of class_exists("array").
14

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.

9 Comments

Yes. But you can always use foreach($array as $key => &$value) {} and it will give you a reference to the element.
@KonstantinBodnya, you totally missed the point of my answer. If you need to alter the array (not the element) you must use each().
passing a reference to the element in the array will alter the array.
@AndrewWillis, it will alter the element in the array but not the array itself. In other words, you can't do things like remove the element, and thereby alter the actual array of items.
@A.L, look at the "print $i" output to see the difference. The important thing here is to understand about the modification to the actual array currently being iterated over (each()) vs the modification to the final output array (foreach())
|
1

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

What exactly is in $array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.