而(名单($键,$值)=每($阵列))与的foreach($数组$关键=> $值)?

而(名单($键,$值)=每($阵列))与的foreach($数组$关键=> $值)?

问题描述:

最近,我遇到这个奇怪的问题:

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)

...完美。

和,我很好奇,现在..是这两个之间的区别?

And, I'm curious now.. what is the difference between those two?

要是你previously遍历数组? 每()记住其在数组中的位置,所以如果你没有复位()它,你可以错过的项目

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

有关它的价值遍历数组这种方法是古老而由更地道的foreach已被取代。我不会使用它,除非你特别想利用它的一个项目,在-A-实时性的优势。

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.

(来源: PHP手册