dongpa3109 2014-01-18 10:57
浏览 204
已采纳

如何检查数组中的每个值是空的?

If I have an array:

$nav = array($nav_1, $nav_2, $nav_3);

and want to check if they are empty with a loop (the real array is much bigger), so that it checks each variable separately, how do I do it?

I want something like this;

$count = 0;

while(count < 3){
     if(empty($nav[$count])) //the loops should go through each value (nav[0], nav[1] etc.)
          //do something
          $count = $count+1;
     }else{
          //do something
          $count = $count+1;
     }
}
  • 写回答

3条回答 默认 最新

  • dongwen7380 2014-01-18 10:59
    关注

    Pretty straight-forward with a foreach loop:

    $count = 0;
    foreach ($nav as $value) {
        if (empty($value)) {
            // empty
            $count++;
        } else {
            // not empty
        }
    }
    
    echo 'There were total ', $count, ' empty elements';
    

    If you're trying to check if all the values are empty, then use array_filter():

    if (!array_filter($nav)) {
        // all values are empty
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度