user1187135
user1187135

Reputation:

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

Is there a shortcut to check if none of the values in the array are empty. I don't want to have to list it out one by one.

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

Upvotes: 1

Views: 130

Answers (2)

user1043994
user1043994

Reputation:

if (has_empty($form_inputs)) {
    // header location
}

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

Upvotes: 2

Zbigniew
Zbigniew

Reputation: 27604

Use in_array:

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

in_array will recognize '', null, 0, false as empty, so it may not work too well, depending on your values. This is typically good for checking string arrays.

Upvotes: 4

Related Questions