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

时间:2021-10-19 01:43:28

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.

当使用空时我也会得到第一个数组值为假的零,而isset似乎没有这个技巧。

6 个解决方案

#1


24  

use array_key_exists() and is_null() for that. It will return TRUE if the key exists and has a value far from NULL

使用array_key_exists()和is_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";
}

#2


13  

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. Unfortunately the first parameter in in_array has to be a string and cannot be an array of values.

请注意,将第三个参数设置为true意味着严格比较,这意味着0将不等于null - 但是,也不会清空字符串('') - 这就是我们有两个条件的原因。不幸的是,in_array中的第一个参数必须是一个字符串,不能是值数组。

#3


5  

PHP empty return values states:

PHP空返回值状态:

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

如果var存在且具有非空的非零值,则返回FALSE。否则返回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; (a variable declared, but without a value)

是$ var; (声明的变量,但没有值)

From your array example I take it as you want to exclude the 0 as an integer. If that's the case this would do the trick:

从您的数组示例中我可以将其作为整数排除0。如果是这样的话就可以解决这个问题:

<?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. Take in account that this might not be the optimal solution if you want to check other values.

如果你想排除其他条件,那么空认为只是在那种情况下否定它们。请注意,如果要检查其他值,这可能不是最佳解决方案。

#4


2  

if ( !isset($array[$key]) || $array[$key] == "" || is_null($array[$key]) )
{
    //given key does not exist or it has "" or NULL value
}

#5


1  

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
}

#6


1  

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. Which may be what you're running into (aside from 0 being considered empty) Compare with this usage:

至于使用isset() - 考虑设置一个空字符串。这可能是你遇到的(除了0被认为是空的)与这种用法比较:

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

print_r($foo);
foreach ($foo as $key => $val) {
        if (isset($foo[$key])) {
                echo $key.", true\n";
        }
}

#1


24  

use array_key_exists() and is_null() for that. It will return TRUE if the key exists and has a value far from NULL

使用array_key_exists()和is_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";
}

#2


13  

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. Unfortunately the first parameter in in_array has to be a string and cannot be an array of values.

请注意,将第三个参数设置为true意味着严格比较,这意味着0将不等于null - 但是,也不会清空字符串('') - 这就是我们有两个条件的原因。不幸的是,in_array中的第一个参数必须是一个字符串,不能是值数组。

#3


5  

PHP empty return values states:

PHP空返回值状态:

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

如果var存在且具有非空的非零值,则返回FALSE。否则返回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; (a variable declared, but without a value)

是$ var; (声明的变量,但没有值)

From your array example I take it as you want to exclude the 0 as an integer. If that's the case this would do the trick:

从您的数组示例中我可以将其作为整数排除0。如果是这样的话就可以解决这个问题:

<?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. Take in account that this might not be the optimal solution if you want to check other values.

如果你想排除其他条件,那么空认为只是在那种情况下否定它们。请注意,如果要检查其他值,这可能不是最佳解决方案。

#4


2  

if ( !isset($array[$key]) || $array[$key] == "" || is_null($array[$key]) )
{
    //given key does not exist or it has "" or NULL value
}

#5


1  

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
}

#6


1  

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. Which may be what you're running into (aside from 0 being considered empty) Compare with this usage:

至于使用isset() - 考虑设置一个空字符串。这可能是你遇到的(除了0被认为是空的)与这种用法比较:

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

print_r($foo);
foreach ($foo as $key => $val) {
        if (isset($foo[$key])) {
                echo $key.", true\n";
        }
}