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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?