2

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

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
Kairos
  • 23
  • 4
  • 1
    Rizier123, not necessarily the same question. – George Jul 22 '15 at 12:28
  • @George Why shouldn't it be? It shows exactly how to check, that an array only contains the same values – Rizier123 Jul 22 '15 at 12:29
  • Array contains the same values vs Array contains the same of specific value, no? – George Jul 22 '15 at 12:30
  • I agree with George since this goes deeper as working with booleans changes this question enough to be considered different. – John Conde Jul 22 '15 at 12:31
  • @George wow only to change: `if (count(array_unique($allvalues)) === 1 && end($allvalues) === 'true') {` to `if (count(array_unique($allvalues)) === 1 && end($allvalues) === FALSE) {` makes it a different question? – Rizier123 Jul 22 '15 at 12:32
  • Well, yeah.. Having said that, the OP has shown no (research) effort of their own, so if anything it should be closed as off-topic. – George Jul 22 '15 at 12:35
  • @George So you really want to tell me, that changing one value in the entire code makes it not a duplicate?! – Rizier123 Jul 22 '15 at 12:36
  • IMO yeah. While that change to get the solution might be obvious to you or me, it might not be to the person asking the question. – George Jul 22 '15 at 12:38
  • @George So you wouldn't close any question with this: http://stackoverflow.com/a/8881719/3933332 only because they have to change one freaking value. So we would end up with thousands of `in_array()` answers just with different values. I completely disagree here, but don't want to discuss here in the comments, so I will end it here. – Rizier123 Jul 22 '15 at 12:44

3 Answers3

3

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)));

Demo

If you want 0 to be considered false just remove the callback to 'strlen' in array_filter().

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

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)
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
-1

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;
}
Ehab Eldeeb
  • 722
  • 4
  • 12