欢迎各位兄弟 发布技术文章
这里的技术是共享的
1.跳出内循环
在内循环里使用break只会跳出单层循环,并不会影响外层循环工作。
function test2()
{
$a = [1,2,3,5,6,7,8,9];
$b = [3,5,6,7,10,8];
foreach ($a as $value){
foreach ($b as $v){
if($value == $v){
var_dump($v); // 会输出:3、5、6、7、8
break;
}
}
echo $value; //12356789
}
}
1.跳出内循环继续外循环
在循环内 continue或break 后加数字可以指定跳出几层循环,比如 continue 2; 就是跳出2层循环
function test2()
{
$a = [1,2,3,5,6,7,8,9];
$b = [3,5,6,7,10,8];
foreach ($a as $value){
foreach ($b as $v){
if($value == $v){
continue 2;
}
}
echo $value; //129
}
}
function test3()
{
$a = [1,2,3,5,6,7,8,9];
$b = [3,5,6,7,10,8];
foreach ($a as $value){
foreach ($b as $v){
if($value == $v){
break 2;
}
}
echo $value; //12
}
}
来自 https://blog.csdn.net/qq_39004843/article/details/109644863