【问题标题】:How to go to next record in foreach loop如何在foreach循环中转到下一条记录
【发布时间】:2011-08-07 04:04:24
【问题描述】:

在下面的代码中,如果$getd[0]为空,我想去下一条记录。

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

我怎样才能实现它?

【问题讨论】:

    标签: php loops foreach explode


    【解决方案1】:

    我们可以使用 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];
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 好吧,当你获得那个声誉时,也许你可以回来:) 很高兴这是你想要的。
      猜你喜欢
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多