1. Jump out of the inner loop
Using break in the inner loop will only jump out of the single-layer loop, and will not affect the work of the outer loop.
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. Jump out of the inner loop to continue the outer loop
Add a number after continue or break in the loop to specify how many levels to jump out of the loop, such as continue 2; it means to jump out of the two-level loop
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
}
}