简体   繁体   English

如何检查关联数组是否具有空值或 null 值

[英]How to check if an associative array has an empty or null value

In the following associative array在下面的关联数组中

$array = array(
    [0] => 0
    [1] => 1
    [2] => 
    [3] => 2
    [4] => 
)

how can you determine if a given key has an empty (or null) value?如何确定给定键是否具有空(或空)值? I used我用了

if(empty($array[$value])) 

and

if(isset($array[$value])) && $array[$value] !=='')

When using empty I also get false for the first array value which is zero and isset doesn't seem to do the trick.当使用empty时,第一个数组值也为false ,它是零,而isset似乎没有用。

use array_key_exists() and is_null() for that. 使用array_key_exists()is_null() It will return TRUE if the key exists and has a value far from NULL 如果密钥存在并且其值远离NULL ,则它将返回TRUE

Difference: 区别:

$arr = array('a' => NULL);

var_dump(array_key_exists('a', $arr)); // -->  TRUE
var_dump(isset($arr['a'])); // -->  FALSE

So you should check: 所以你应该检查:

if(array_key_exists($key, $array) && is_null($array[$key])) {
    echo "key exists with a value of NULL";
}

Looked at all the answers and I don't like them. 看着所有答案,我不喜欢他们。 Isn't this much simpler and better? 这不是更简单,更好吗? It's what I am using: 这就是我正在使用的:

  if (in_array(null, $array, true) || in_array('', $array, true)) {
    // There are null (or empty) values.
  }

Note that setting the third parameter as true means strict comparison, this means 0 will not equal null - however, neither will empty strings ('') - this is why we have two conditions. 请注意,将第三个参数设置为true意味着严格比较,这意味着0将不等于null - 但是,也不会清空字符串('') - 这就是为什么我们有两个条件。 Unfortunately the first parameter in in_array has to be a string and cannot be an array of values. 不幸的是,in_array中的第一个参数必须是一个字符串,不能是值数组。

PHP empty return values states: PHP 返回值状态:

Returns FALSE if var exists and has a non-empty, non-zero value. 如果var存在且具有非空的非零值,则返回FALSE。 Otherwise returns TRUE. 否则返回TRUE。

The following things are considered to be empty: 以下内容被认为是空的:

"" (an empty string) “”(空字符串)

0 (0 as an integer) 0(0为整数)

0.0 (0 as a float) 0.0(0作为浮点数)

"0" (0 as a string) “0”(0作为字符串)

NULL 空值

FALSE

array() (an empty array) array()(一个空数组)

$var; 是$ var; (a variable declared, but without a value) (声明的变量,但没有值)

From your array example I take it as you want to exclude the 0 as an integer . 从你的数组示例中我可以将它作为整数排除0 If that's the case this would do the trick: 如果是这样的话就可以解决这个问题:

<?php
    $array = array(0, 1, '', 2, '');

    foreach ($array as $value) {
        echo (empty($value) && 0 !== $value) ? "true\n" : "false\n";
    }

If you want to exclude other conditions that empty considers just negate them in that condition. 如果你想排除其他条件,那么empty认为只是在那种情况下否定它们。 Take in account that this might not be the optimal solution if you want to check other values. 请注意,如果要检查其他值,这可能不是最佳解决方案。

if ( !isset($array[$key]) || $array[$key] == "" || is_null($array[$key]) )
{
    //given key does not exist or it has "" or NULL value
}
foreach($array as $i => $v) {
    if(null === $v) {
        // this item ($array[$i]) is null
    }
}

...or, for a given key: ......或者,对于给定的密钥:

if(null === $array[2]) {
     // this item ($array[2]) is null
}

Potentially this could be cleaner if I knew how the array was constructed, but, having the assumption that you can have both empty strings, or nulls in the array, and you want to account for values of 0 --> here's what I'd do: 如果我知道数组是如何构造的,这可能会更清晰,但是,假设你可以同时拥有空字符串或数组中的空值,并且你想要考虑0的值 - >这就是我的意思做:

if (is_null($array[$key]) || (string)$array[$key] == '')

Here's a little bit of test code showing it in action with an array that has both 0, null, an empty string, and non-zero integers... 这里有一些测试代码,它显示了一个包含0,null,空字符串和非零整数的数组的动作...

$array = array(0,1,null,2,'');
print_r($array);

foreach ($array as $key => $val) {
         if (is_null($array[$key]) || (string)$array[$key] == '') {
           echo $key.", true\n";
         }
}

As for using isset() -- an empty string is consider to be set. 至于使用isset() - 考虑设置一个空字符串。 Which may be what you're running into (aside from 0 being considered empty) Compare with this usage: 这可能是你遇到的(除了0被认为是空的)与这种用法比较:

$foo = array(0,1,null,2,'');

print_r($foo);
foreach ($foo as $key => $val) {
        if (isset($foo[$key])) {
                echo $key.", true\n";
        }
}
 function is_empty($data){
    $is_empty = true;
    foreach ($data as $val){
       if(is_array($val)){
          $is_empty = is_empty($val);
       }else{
          if(!empty($val)){
             $is_empty = false;
             break;
          }
       }
    }
    return $is_empty;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM