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.