如何在foreach循环中转到下一个记录


How to go to next record in foreach loop

在下面的代码中,如果$getd[0]为空,我想转到下一个记录

foreach ($arr as $a1) {
  $getd = explode(',' ,$a1);
  $b1 = $getd[0];
}

我怎样才能实现它?

我们可以使用if语句只在$getd[0]不为空时才发生某些事情。

foreach ($arr as $a1) {
    $getd=explode(",",$a1);
    if (!empty($getd[0])) {
        $b1=$getd[0];
    }
}

或者,如果$getd[0]为空,我们可以使用continue关键字跳到下一个迭代。

foreach ($arr as $a1) {
    $getd=explode(",",$a1);
    if (empty($getd[0])) {
        continue;
    }
    $b1=$getd[0];
}

使用continue,它将跳转到下一次循环。

foreach ($arr as $a1){
    $getd=explode(",",$a1);

    if(empty($getd[0])){
        continue;
    }
    $b1=$getd[0];
}