而(名单($键,$值)=每($阵列))与的foreach($数组$关键=> $值)? [英] while(list($key, $value) = each($array)) vs. foreach($array as $key => $value)?

查看:128
本文介绍了而(名单($键,$值)=每($阵列))与的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手册

这篇关于而(名单($键,$值)=每($阵列))与的foreach($数组$关键=> $值)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆