Home » Php » php – How to go to next record in foreach loop

php – How to go to next record in foreach loop

Posted by: admin April 23, 2020 Leave a comment

Questions:
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.

How to&Answers:

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

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

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

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

Answer:

Using continue which will skip to the next iteration of the loop.

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


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

    $b1=$getd[0];

}