du7535 2017-09-29 19:58
浏览 70
已采纳

我可以不在PHP中的foreach后循环数组吗?

I've started teaching myself php and have found something I don't understand. If it is really simple, my apologies! I'm not seeing what I'm doing wrong, or if it is a quirk of foreach loops it would be useful to know what and why. I was trying this out purely as an example to see how the loops work in php, so I just added them one after another in a file.

My code (based off an example in a book):

<?php

$p = array(
    'Copier' => "Numero Uno",
    'Inkjet' => "Numero Dos",
    'Laser' => "Numero Tres",
    'Photo' => "Numero Cuatro"
);

foreach ($p as $item => $desc) {
    echo "$item: $desc<br />";
}

echo "<br />";
echo "---";
echo "<br /><br />";

while (list($item2, $desc2) = each($p)) {
    echo "$item2: $desc2<br />";
}

With this code I get this output:

Copier: Numero Uno
Inkjet: Numero Dos
Laser: Numero Tres
Photo: Numero Cuatro

---

If I swap the foreach and while loops I get this (which is what I expect):

Copier: Numero Uno
Inkjet: Numero Dos
Laser: Numero Tres
Photo: Numero Cuatro

---

Copier: Numero Uno
Inkjet: Numero Dos
Laser: Numero Tres
Photo: Numero Cuatro

Because I've not been getting an output for the while if it runs after the foreach, I renamed item and desc just in case (hence why $item2, $desc2).

While writing this it occurred to me to make a new variable array from $p and try with that ($p2).

$p2 = $p;

while (list($item2, $desc2) = each($p2)) {
    echo "$item2: $desc2<br />";
}

This works. I also tried repeating the foreach loop again exactly the same (copy and pasted to where the while was) and that works.

Does the foreach somehow arrange the $p array in a way that the while doesn't understand? What is going on here? Is there a useful way to see what is going on under the hood in php?

  • 写回答

1条回答 默认 最新

  • dousikuai5417 2017-09-29 20:07
    关注

    After testing on PHP7 I could not replicate your problem, but if you're running on an earlier version of PHP you need to reset() the array pointer back to the beginning of the array:

    $p = array(
        'Copier' => "Numero Uno",
        'Inkjet' => "Numero Dos",
        'Laser' => "Numero Tres",
        'Photo' => "Numero Cuatro"
    );
    
    foreach ($p as $item => $desc) {
        echo "$item: $desc<br />";
    }
    
    reset($p);
    
    echo "<br />";
    echo "---";
    echo "<br /><br />";
    
    while (list($item, $desc) = each($p)) {
        echo "$item: $desc<br />";
    }
    

    This produces (example):

    Copier: Numero Uno
    Inkjet: Numero Dos
    Laser: Numero Tres
    Photo: Numero Cuatro

    ---

    Copier: Numero Uno
    Inkjet: Numero Dos
    Laser: Numero Tres
    Photo: Numero Cuatro

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效