本文介绍了检查数组值是否设置为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查数组变量
$a = array('a'=>1, 'c'=>null);
已设置且为空。
function check($array, $key)
{
if (isset($array[$key])) {
if (is_null($array[$key])) {
echo $key . ' is null';
}
echo $key . ' is set';
}
}
check($a, 'a');
check($a, 'b');
check($a, 'c');
在PHP中是否有可能会检查$ a ['c']是否为null的函数并且如果$ a ['b']存在而没有 PHP注意:...错误?
Is it possible in PHP to have function which will check if $a['c'] is null and if $a['b'] exist without "PHP Notice: ..." errors?
推荐答案
使用而不是 isset()
,因为如果变量为,
,而 isset()
将返回 false
null array_key_exists()
只是检查键是否存在于数组中:
Use array_key_exists()
instead of isset()
, because isset()
will return false
if the variable is null
, whereas array_key_exists()
just checks if the key exists in the array:
function check($array, $key)
{
if(array_key_exists($key, $array)) {
if (is_null($array[$key])) {
echo $key . ' is null';
} else {
echo $key . ' is set';
}
}
}
这篇关于检查数组值是否设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!