简体   繁体   中英

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.

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

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

PHP empty return values states:

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

The following things are considered to be empty:

"" (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

"0" (0 as a string)

NULL

FALSE

array() (an empty array)

$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 . 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. 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:

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...

$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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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