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

41
在下面的代码中,如果$getd[0]为空,我想跳到下一条记录。
foreach ($arr as $a1) {
  $getd = explode(',' ,$a1);
  $b1 = $getd[0];
}

我该如何实现它?

2个回答

64
我们可以使用if语句来确保只有当$getd[0]不为空时才会执行某些操作。
foreach ($arr as $a1) {
    $getd=explode(",",$a1);
    if (!empty($getd[0])) {
        $b1=$getd[0];
    }
}

或者,我们可以使用 continue 关键字,在$getd[0]为空时跳过到下一次迭代。

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

39

使用continue语句,它将跳过当前迭代并进入下一次循环。

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


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

    $b1=$getd[0];

}

3
也许当你赢得那个声誉时,你可以回来 :) 很高兴这正是你想要的。 - Mike Lewis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接