如何检查数组中的每个值是空的? [英] How to check each value in an array is empty?

查看:137
本文介绍了如何检查数组中的每个值是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个数组:

  $ = NAV阵列($ nav_1,$ nav_2,$ nav_3);

和要检查它们是否为空以循环(真正的数组是更大),因此它单独检查每个变量,我该怎么办呢?

我想是这样的;

  $计数= 0;而(数3;){
     如果(空($ NAV [$计数))//环路应该通过每个值(NAV [0],NAV [1]等)
          //做一点事
          $计数= $计数+ 1;
     }其他{
          //做一点事
          $计数= $计数+ 1;
     }
}


解决方案

pretty直接的与 的foreach 循环:

  $计数= 0;
的foreach($ NAV为$值){
    如果(空($值)){
        //空
        $计数++;
    }其他{
        // 不是空的
    }
}回声有总',$算,'空元素;

如果你想检查是否全部的值是空的,然后使用 array_filter()

 如果(!array_filter(NAV $)){
    //所有值都为空
}

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

解决方案

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
}

这篇关于如何检查数组中的每个值是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆