duankuai6991 2015-10-28 11:54
浏览 23
已采纳

Foreach与list-each在PHP中

Is there any difference in the following methods?

$fruit = array(
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cranberry'
);

// List-each method
reset($fruit);
while (list($key, $val) = each($fruit)) {
    echo "$key => $val
";
}

// foreach method
foreach ($fruit as $key => $value) {
    echo "$key => $val
";
}
  • 写回答

3条回答 默认 最新

  • duanpuchen3142 2015-10-28 11:56
    关注

    list start iteration from current element whereas foreach start from the first element.

    In case you've already iterated the array, list will start from the next element from the previous iteration. You need to reset the array if you want to iterate from the first element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?