问题
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 :)
回答1:
if(count(array_unique($your_array)) === 1)
return current($your_array);
else return;
回答2:
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';
}
回答3:
One can use array_product
as demonstrated at php.net in a comment:
$check[] = boolval(TRUE);
$check[] = boolval(1);
$check[] = boolval(FALSE);
$check[] = boolval(0);
$result = (bool) array_product($check);
// $result is set to FALSE because only two of the four values evaluated to TRUE
回答4:
This question is ancient, but no matter. I wanted to benchmark the different approaches. The in_array()
method performs best since it probably doesn't need to iterate through the whole array. (Odds are low you'd have but one unique boolean at the end, but even then it performs well.)
One approach not mentioned here is array_sum($array)
, which returns 0
if all values are false
, and 1+
if there's a true
value somewhere. Like the array_filter
approach, it won't tell you if both are present, but is useful for finding out if anything is true
. I also added in a basic foreach
check for mixed or all true/false, and an in_array
that just checks if anything is true
(as *_bin
below).
So here are the benchmarks. Each case is iterated 100000 times with an array of 10, 100 or 1000 random booleans; and again with 9, 99 and 999 identical booleans, with the last one unique (to have full iteration time for in_array
). First three checks tested produce the requested true/false/both
result, and the remaining four simply check if a true
value is present.
RANDOM BOOLEANS
- in_array: 10 bools = 0.16 sec
- foreach: 10 bools = 0.122 sec
- array_unique: 10 bools = 0.274 sec
- foreach_bin: 10 bools = 0.095 sec
- in_array_bin: 10 bools = 0.073 sec
- array_sum: 10 bools = 0.074 sec
- array_filter: 10 bools = 0.118 sec
- in_array: 100 bools = 0.153 sec
- foreach: 100 bools = 0.122 sec
- array_unique: 100 bools = 2.3451 sec
- foreach_bin: 100 bools = 0.094 sec
- in_array_bin: 100 bools = 0.074 sec
- array_sum: 100 bools = 0.126 sec
- array_filter: 100 bools = 0.228 sec
- in_array: 1000 bools = 0.154 sec
- foreach: 1000 bools = 0.149 sec
- array_unique: 1000 bools = 32.6659 sec (!!)
- foreach_bin: 1000 bools = 0.075 sec
- in_array_bin: 1000 bools = 0.074 sec
- array_sum: 1000 bools = 0.8771 sec
- array_filter: 1000 bools = 1.4021 sec
LAST BOOLEAN DIFFERS
- in_array: 10 bools = 0.152 sec
- foreach: 10 bools = 0.342 sec
- array_unique: 10 bools = 0.269 sec
- foreach_bin: 10 bools = 0.074 sec
- in_array_bin: 10 bools = 0.076 sec
- array_sum: 10 bools = 0.074 sec
- array_filter: 10 bools = 0.121 sec
- in_array: 100 bools = 0.159 sec
- foreach: 100 bools = 2.8072 sec
- array_unique: 100 bools = 2.7702 sec
- foreach_bin: 100 bools = 0.074 sec
- in_array_bin: 100 bools = 0.09 sec
- array_sum: 100 bools = 0.118 sec
- array_filter: 100 bools = 0.248 sec
- in_array: 1000 bools = 0.312 sec
- foreach: 1000 bools = 27.5256 sec
- array_unique: 1000 bools = 42.1594 sec
- foreach_bin: 1000 bools = 0.074 sec
- in_array_bin: 1000 bools = 0.24 sec
- array_sum: 1000 bools = 0.555 sec
- array_filter: 1000 bools = 1.3601 sec
Then, in summary. The array_unique
way clearly holds the tail, don't use for large arrays or large volumes of arrays! The foreach
way has a slight edge over in_array
, but alas the code isn't quite as elegant. The array_sum
way is on par with the "if true" checks for smaller (<100) arrays. (I dig the simplicity in array_sum($array) > 0
.) The array_filter
way lags a bit behind in_array
and foreach
. When only the last value differs, foreach
and array_unique
both drag it bad.
Finally, the foreach
function for good humors. Very readable. The truth is out there!
function foreach_truth_test($array)
{
$trues = $falses = false;
foreach($array as $val) {
if ($val === true) {
$trues = true;
} elseif ($val === false) {
$falses = true;
}
if ($trues === true && $falses === true) {
return 'both'; // Enough information.
}
}
// Regular Universe
if ($trues === true && $falses === false) {
return 'true';
} // Evil Mirror Universe
elseif ($trues === false && $falses === true) {
return 'false';
} // Intergalactic Void
else {
return 'void'; // =^_^=
}
}
P.S. Could use 'null' above, but it reads more fun this way around. Benchmarked on Windows so the microtime readings are chunky.
回答5:
If you store only booleans, use this:
$a = array('a'=> true, 'b'=> true, 'c'=>true);
$af = array_filter($a);
if ($af == $a) {
echo "all true";
}
if (empty($af)) {
echo "all false";
}
Note: if you have other values in the array they will be converted to boolean according to the horrific conversion rules of PHP.
回答6:
A simple loop will do. Mind that if the array is empty, both conditions are met (all false and all true). You won't see this in the result, because of the else, but you can find out yourself how you want to handle this.
// Input
$x = array ('a'=>false, 'b'=>false, 'c'=>false);
// Initialization
$anytrue = false;
$alltrue = true;
// Processing
foreach($x as $k=>$v)
{
$anytrue |= $v;
$alltrue &= $v;
}
// Display result
if ($alltrue)
echo 'All elements are true';
elseif (!$anytrue)
echo 'All elements are false';
else
echo 'Mixed values';
回答7:
If they're all array elements, with true/false values, then use array_flip():
$new = array_flip($array);
if (!isset($array[false]) && isset($array[true])) {
... there's no false values, and at least one true value
}
This could get expensive for a large array, so you may want to try array_unique()
instead. You'd get an array with at most two values (one true, one false).
Ok, so that wouldn't work. Simple shotgun approach:
if (in_array($yourarray, false, TRUE)) {
... at least one non-true value
}
回答8:
More over you can have a list o variables (not only an array of values) to check all the values against a certain value.
$var1 = true;
$var2 = true;
$var3 = false;
$isAllVarsTrue = !in_array(false, [$var1, $var2, $var3], true);
var_dump($isAllVarsTrue); //false
回答9:
Use a for loop. If you want to check that all variables are false you can use a for loop: once you find a true element you can break the cycle, otherwise variables are all false. Same method you can use if you want to check that all variables are true.
回答10:
// set default value
$result = false;
foreach ($array as $key => $value) {
if ($value === true) {
$result = true;
break;
}
}
// $result is now true if any value was true, otherwise it's false
来源:https://stackoverflow.com/questions/6850452/check-if-multiple-values-are-all-false-or-all-true