Kairos
Kairos

Reputation: 43

How to check if an array contains all false values?

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

Upvotes: 4

Views: 2253

Answers (4)

k0pernikus
k0pernikus

Reputation: 66500

I had the use case to check for this in a phpunit testcase.

I went with the approach:

    public function areOnlySameValuesInArray(mixed $expectedValue, array $haystack): bool
    {
        $haystack = array_unique($haystack);

        if (count($haystack) !== 1) {
            return false;
        }

        return reset($haystack) === $expectedValue;
    }

This is only relevant for my use case to set up a custom assertion for PHP Unit.

I extended the TestCase class and added helper assertions:

<?php

namespace Test;

use PHPUnit\Framework\TestCase;

class MstTestCase extends TestCase
{
    public static function assertArrayOnlyContainsTrue(array $haystack): void
    {
        static::assertArrayOnlyContainsSameValue(true, $haystack);
    }

    public static function assertArrayOnlyContainsFalse(array $haystack)
    {
        static::assertArrayOnlyContainsSameValue(false, $haystack);

    }

    public static function assertArrayOnlyContainsSameValue(mixed $expectedValue, array $haystack): void
    {
        $haystack = array_unique($haystack);

        static::assertTrue(static::areOnlySameValuesInArray($expectedValue, $haystack), message: 'The array contains of different values, yet sameness was expected');
    }

    private static function areOnlySameValuesInArray(mixed $expectedValue, array $haystack): bool
    {
        $haystack = array_unique($haystack);

        if (count($haystack) !== 1) {
            return false;
        }

        return reset($haystack) === $expectedValue;
    }
}

Once your test case extends the file, you have access to the assertions methods.

Here is a contrived example so that you can see that this works:

class SomeTest extends MstTestCase {
    /**
     * @dataProvider arrayProvider
     */
    public function testArrays($haystack)
    {
        $this->assertArrayOnlyContainsFalse($haystack);
    }

    public function arrayProvider()
    {
        yield 'all_false' => [[false, false, false]]; // passes
        yield 'empty' => [[]]; // fails
        yield 'some_false_but_not_all' => [[false, false, true]]; // fails
        yield 'all_falsey' =>  [[0, 'false', false]]; //fails
    }
}

Output in phpstorm:

Screebshot showing instances of the test passing and for which it fails

Upvotes: 0

John Conde
John Conde

Reputation: 219804

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

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31749

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)

Upvotes: -1

Ehab Eldeeb
Ehab Eldeeb

Reputation: 720

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

Upvotes: -2

Related Questions