php - How to check if an associative array has an empty or null value -
in following associative array
$array = array( [0] => 0 [1] => 1 [2] => [3] => 2 [4] => )
how can determine if given key has empty (or null) value? used
if(empty($array[$value]))
and
if(isset($array[$value])) && $array[$value] !=='')
when using empty
false
first array value 0 , isset
doesn't seem trick.
use array_key_exists()
, is_null()
that. return true
if key exists , has value far null
difference:
$arr = array('a' => null); var_dump(array_key_exists('a', $arr)); // --> true var_dump(isset($arr['a'])); // --> false
so should check:
if(array_key_exists($key, $array) && is_null($array[$key])) { echo "key exists value of null"; }
Comments
Post a Comment