柒伍伍捌壹陆 2022-10-10 17:49 采纳率: 0%
浏览 55

foreach嵌套使用 switch 在switch内使用continue无法跳过当前循坏,如何解决?(语言-php)

foreach嵌套使用 switch 在switch内使用continue无法跳过当前循坏
    public function test() {
        dump('--testa--');
        dump($this->testa()); //continue in switch failure
        dump('--testb--');
        dump($this->testb()); //continue out switch success
        dump('--testc--');
        dump($this->testc()); //use if in stead of switch to make sure it is not caused by if
    }

    private function testa() {
        $returns = [];
        $tests = [
            ['type' => 0, 'check' => 1, 'value' => 4],
            ['type' => 0, 'check' => 1, 'value' => 3],
            ['type' => 1, 'check' => 1, 'value' => 2],
            ['type' => 2, 'check' => 1, 'value' => 1],
        ];
        foreach ($tests as $t => $test) {
            if ($test['check']) {
                switch ($test['type']) {
                    case 0:
                        if ($test['value'] > 2) {
                            continue; //failure
                        }
                        break;
                    case 1:
                        if ($test['value'] > 2) {
                            continue; //failure
                        }
                        break;
                    default :
                        if ($test['value'] > 2) {
                            continue; //failure
                        }
                        break;
                }
            }
            array_push($returns, $test);
        }
        return $returns;
    }


    private function testb() {
        $returns = [];
        $tests = [
            ['type' => 0, 'check' => 1, 'value' => 4],
            ['type' => 0, 'check' => 1, 'value' => 3],
            ['type' => 1, 'check' => 1, 'value' => 2],
            ['type' => 2, 'check' => 1, 'value' => 1],
        ];
        foreach ($tests as $t => $test) {
            $continue = false;
            if ($test['check']) {
                switch ($test['type']) {
                    case 0:
                        if ($test['value'] > 2) {
                            $continue = true;
                        }
                        break;
                    case 1:
                        if ($test['value'] > 2) {
                            $continue = true;
                        }
                        break;
                    default :
                        if ($test['value'] > 2) {
                            $continue = true;
                        }
                        break;
                }
            }
            if($continue){
                continue; //success
            }
            array_push($returns, $test);
        }
        return $returns;
    }
    
    private function testc() {
        $returns = [];
        $tests = [
            ['type' => 0, 'check' => 1, 'value' => 4],
            ['type' => 0, 'check' => 1, 'value' => 3],
            ['type' => 1, 'check' => 1, 'value' => 2],
            ['type' => 2, 'check' => 1, 'value' => 1],
        ];
        foreach ($tests as $t => $test) {
            if ($test['check'] && $test['type'] == 0 && $test['value'] > 2) {
                continue; //success
            }
            if ($test['check']) {
                if ($test['type'] == 1 && $test['value'] > 2) {
                    continue; //success
                }
                if ($test['type'] == 2) {
                    if ($test['value'] > 2) {
                        continue; //success
                    }
                }
            }
            array_push($returns, $test);
        }
        return $returns;
    }

testa返回了4个元素 testb返回了2个元素
实际应该两个都是返回2个元素的

  • 写回答

1条回答 默认 最新

  • 三只小菜猿 PHP领域新星创作者 2022-10-10 17:56
    关注

    return true 或者return false来,break和continue在phpforeach中不生效

    评论

报告相同问题?

问题事件

  • 修改了问题 10月10日
  • 修改了问题 10月10日
  • 修改了问题 10月10日
  • 修改了问题 10月10日
  • 展开全部

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢