如何检查数组中是否没有任何值为空


How do I check if none of the values in the array are empty?

是否有快捷方式可以检查数组中的值是否为空。我不想把它一个接一个地列出来。

$form_inputs = array (
    'name' => $name,
    'gender' => $gender, 
    'location' => $location,
    'city' => $city,
    'description' => $description);
if (!empty(XXXXXXXX)){
        echo 'none are empty';
    } else {
        header('Location:add.school.php?error=1');
        exit();
    }

使用in_array:

if(in_array('', $form_inputs)) {
  echo 'has empty field(s)';
}

in_array会将''null0false识别为空,因此根据您的值,它可能工作不太好。这通常适用于检查字符串数组。

if (has_empty($form_inputs)) {
    // header location
}
function has_empty($array) {
    foreach ($array as $key=>$value) {
        if (empty($value)) {
            return true;
        }
    }
}