How to check if an array contains all false
values?
$arr = array(false, false, false);
$arr2 = array(true, false, false, 123);
check_false_array($arr);
true
check_false_array($arr2);
false
edit: not only true
/false
values are allowed
How to check if an array contains all false
values?
$arr = array(false, false, false);
$arr2 = array(true, false, false, 123);
check_false_array($arr);
true
check_false_array($arr2);
false
edit: not only true
/false
values are allowed
Use array_filter()
and empty()
. array_filter()
will remove all false values and if empty()
returns true you have all false values.
function check_false_array(array $array) {
return empty(array_filter($array, 'strlen'));
}
var_export(check_false_array(array(false, false, false)));
echo "\n";
var_export(check_false_array(array(false, true, false)));
echo "\n";
var_export(check_false_array(array(0,0,0)));
If you want 0
to be considered false
just remove the callback to 'strlen'
in array_filter()
.
You can use array_filter
with a callback for identical match to false
& empty
to check.
$arr = array(false, false, false);
$check = array_filter($arr, function($x) {
return ($x !== false);
});
if(empty($check)) // $check will be empty if all the value is false (only false)
these two functions will check for what you need, assuming the arrays can only have "true" or "false" and nothing else
function isFalseArray($arr){
foreach($arr as $a){
if($a == "true")
return false;
}
return true;
}
function isTrueArray($arr){
foreach($arr as $a){
if($a == "false")
return false;
}
return true;
}