Check if multiple values are all false or all true

Viewed 39786

How can I check if 20 variables are all true, or if 20 variables are all false?

if possible without using a really long if ...

the variables are actually array elements:

array('a'=> true, 'b'=> true ...)

to make it more clear:

  • if the array has both true and false values return nothing
  • if the array has only true values return true
  • if the array has only false values return false :)
11 Answers

Updated

!array_filter($arrayValues ?: [0], fn($v) => !$v);

php > var_dump(!array_filter([1, 2, 3] ?: [0], fn($v) => !$v));
bool(true)

php > var_dump(!array_filter([true] ?: [0], fn($v) => !$v));
bool(true)

php > var_dump(!array_filter([true, false] ?: [0], fn($v) => !$v));
bool(false)

php > var_dump(!array_filter([] ?: [0], fn($v) => !$v));
bool(false)

php > var_dump(!array_filter([false] ?: [0], fn($v) => !$v));
bool(false)
Related