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:
sum(data) represents the addition of 1 and 0 with respective values of True(1) and False(0) in a list. In the case of all False sum is 0, and in the case of all True sum is equal to the length of the list. any sum value other than 0 & 1 means not all is False or True.
To check if all of the values in an array are equal to true , use the every() method to iterate over the array and compare each value to true , e.g. arr. every(value => value === true) . The every method will return true if the condition is met for all array elements. Copied!
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.
The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function also returns True.
if(count(array_unique($your_array)) === 1) return current($your_array); else return;
You could use in_array
Ex. for all true:
if(in_array(false, $array, true) === false){ return true; } else if(in_array(true, $array, true) === false){ return false; } else{ return 'nothing'; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With