PDA

View Full Version : Check if an array contains empty values


guvenck
12-05-2006, 10:41 PM
I send a form via POST method, the form contains an uncertain number of fields, so I use an array (let's say email[]). After sending the form, I would like to check if the array contains empty values. Naturally,

if($_POST['email'] == "") does not work even if all the fields are empty. So, I use a foreach loop to check if the array contains empty values:


$email = $_POST['email'];
foreach ($email as $key => $value){
if ($email[$key] == ""){
echo "field contains empty value";
}
}


is there any other way to check if all the items in an array contain empty values?

ecnarongi
12-05-2006, 11:05 PM
you can use the empty() function. It returns true or false.

ralph l mayo
12-05-2006, 11:22 PM
If the only thing that counts as empty is the empty string:


echo (in_array('', $array)) ? 'has empt(y|ies)' : 'no empties';

guvenck
12-05-2006, 11:35 PM
@ecnarongi

This does not work:


if(empty($_POST['Email'])) {
echo "array is empty";
}



@ralph l mayo

What if I want to check if the whole array is empty?

ralph l mayo
12-06-2006, 12:19 AM
@ralph l mayo

What if I want to check if the whole array is empty?

TMTOWTDI, but:


echo (current($array) == '' && count(array_unique($array)) == 1) ? 'all empty' : 'not empty';