如何检查数组中的空值并返回空值的键?

时间:2014-12-15 10:15:54

标签: php arrays

如何检查数组中的所有键是否都有值。如果它没有任何值,如何打印该密钥。

$value = array(
        [title] => Test Name Four
        [first_name] => 
        [last_name] => M
        [field_company] => ABC
        [email] => 
        [client_phone_number] => 
        [address_line_1] => 
        [address_line_2] => Address 3
        [address_line_3] => 
        [address_line_4] => 
        [post_code] => 
        )

谢谢。

2 个答案:

答案 0 :(得分:2)

您可以使用is_null

$array = array('something' => 0, 'other' => false, 'foo' => null);
foreach ($array as $key => $val) {
    if (is_null($val)) {
        echo $key . " is null!\r\n";
    }
}

答案 1 :(得分:0)

$emptyKeys = array();    
foreach ($arr as $k => $v) {
    if (empty($v) {
        $emptyKeys[$k] = $k;
    }
}
print_r($emptyKeys);