PHP Laravel打破foreach循环2次

时间:2018-01-23 05:30:34

标签: php laravel foreach

有三个foreach循环,在最后一个foreach循环中有一个if条件,

foreach ($candidate_data as $cd) {
        $edu_data=DB::table('applicant_edu_info')
        ->where('applicant_id',$cd->ap_id)
        ->get();
            foreach($edu_data as $ed){
                foreach($neededDegree as $nd){
                    if($neededDegree->edu_degree_id==$ed->edu_degree_id){
                        $education_data[$cd->ap_id]=$neededDegree->name;

                    }
                }
            }
    }

我需要的是,如果条件为真,那么我想打破两个循环并继续从第一个foreach循环运行。请帮忙。

2 个答案:

答案 0 :(得分:5)

在PHP中,您可以使用break的可选参数。

使用source = 1011 1110 1001 0011 mask = 0110 0011 1000 0101 s = pext(source, mask) s -> 0000 0000 0011 0101

break 2;

foreach ($candidate_data as $cd) { $edu_data=DB::table('applicant_edu_info') ->where('applicant_id',$cd->ap_id) ->get(); foreach($edu_data as $ed){ foreach($neededDegree as $nd){ if($neededDegree->edu_degree_id==$ed->edu_degree_id){ $education_data[$cd->ap_id]=$neededDegree->name; break 2; } } } } 将打破两个循环。

文档:http://php.net/manual/en/control-structures.break.php

答案 1 :(得分:1)

您可以使用goto用于跳转到该计划中的另一个section

foreach ($candidate_data as $cd) {
    $edu_data=DB::table('applicant_edu_info')
    ->where('applicant_id',$cd->ap_id)
    ->get();
        foreach($edu_data as $ed){
            foreach($neededDegree as $nd){
                if($neededDegree->edu_degree_id==$ed->edu_degree_id){
                    $education_data[$cd->ap_id]=$neededDegree->name;
                    goto targetLocation;

                }
            }
        }
      targetLocation:
}

使用goto,您可以在任意位置设置target