1

是否有快捷方式来检查数组中的值是否为空。我不想一一列举。

$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();
    }
4

2 回答 2

4

使用in_array

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

in_array'', null, 0,识别false为空,因此它可能无法很好地工作,具体取决于您的值。这通常适用于检查字符串数组。

于 2012-07-14T11:24:36.183 回答
2
if (has_empty($form_inputs)) {
    // header location
}

function has_empty($array) {
    foreach ($array as $key=>$value) {
        if (empty($value)) {
            return true;
        }
    }
}
于 2012-07-14T11:25:11.527 回答