小编ogu*_*gur的帖子

在foreach中切换的bug

这是我遇到问题的代码 - 简单的foreach迭代数组.有时来自数组的元素可以具有键"mailto" - 在这种情况下它应该执行一些任务.但由于某种原因,带有"0"键的第一个元素也进入"mailto"情况.

<?php
    foreach (
        array(
            " this shouldnt appear, it's without mailto as key - 1",
            " this shouldnt appear, it's without mailto as key - 2",
            "mailto" => " this should appear ",
            " this shouldnt appear, it's without mailto as key - 3",
        ) as $type => $text
    ) {
        echo "#$type#\n";
        switch ($type) {
            case "mailto":
                echo ">$type< $text\n";

                break;

            default:
                break;
        }

    }
Run Code Online (Sandbox Code Playgroud)

输出:

#0#
>0<  this shouldnt appear, it's without mailto …
Run Code Online (Sandbox Code Playgroud)

php foreach switch-statement

1
推荐指数
1
解决办法
154
查看次数

标签 统计

foreach ×1

php ×1

switch-statement ×1