How to go to next record in foreach loop

Aryan picture Aryan · Apr 17, 2011 · Viewed 68k times · Source
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.

Answer

erisco picture erisco · Apr 17, 2011

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];
}