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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5691266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:13:56  来源:igfitidea点击:

How to go to next record in foreach loop

phploopsforeachexplode

提问by Aryan

foreach ($arr as $a1){

    $getd=explode(",",$a1);

    $b1=$getd[0];

}

In above code, if that $getd[0]is empty i want to go to next record.

在上面的代码中,如果它$getd[0]是空的,我想转到下一条记录。

回答by erisco

We can use an if statement to only cause something to happen if $getd[0]is not empty.

我们可以使用 if 语句来仅$getd[0]在不为空的情况下导致某些事情发生。

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

Alternatively, we can use the continuekeyword to skip to the next iteration if $getd[0]is empty.

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

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

回答by Mike Lewis

Using continuewhich will skip to the next iteration of the loop.

使用continuewhich 将跳到循环的下一次迭代。

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


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

    $b1=$getd[0];

}