php 检查多个值是否全部为假或全部为真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6850452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if multiple values are all false or all true
提问by sandy
How can I check if 20 variables are all true, or if 20 variables are all false?
如何检查 20 个变量是否全部为真,或者 20 个变量是否全部为假?
if possible without using a really long if ...
如果可能的话,不使用很长的 if ...
the variables are actually array elements:
变量实际上是数组元素:
array('a'=> true, 'b'=> true ...)
数组('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 :)
- 如果数组同时具有 true 和 false 值,则不返回任何内容
- 如果数组只有真值,则返回真
- 如果数组只有 false 值,则返回 false :)
回答by barfoon
if(count(array_unique($your_array)) === 1)
return current($your_array);
else return;
回答by afuzzyllama
回答by lumbric
One can use array_product
as demonstrated at php.net in a comment:
可以array_product
在php.net 的评论中使用:
$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
回答by Markus AO
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.)
这个问题很古老,但无所谓。我想对不同的方法进行基准测试。该in_array()
方法性能最佳,因为它可能不需要遍历整个数组。(几率很低,你最后只有一个唯一的布尔值,但即便如此,它也表现良好。)
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).
这里没有提到的一种方法是array_sum($array)
,0
如果所有值都是false
,并且某处1+
有值,则返回true
。与该array_filter
方法一样,它不会告诉您两者是否都存在,但对于找出是否存在true
. 我还添加了foreach
对混合或所有真/假的基本检查,并且in_array
只检查是否有任何内容true
(如下*_bin
所示)。
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.
所以这里是基准。每个案例都使用 10、100 或 1000 个随机布尔值的数组迭代 100000 次;再次使用 9、99 和 999 个相同的布尔值,最后一个是唯一的(具有完整的迭代时间in_array
)。测试的前三项检查产生请求的true/false/both
结果,其余四项只是检查true
值是否存在。
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:10 个布尔值 = 0.16 秒
- foreach:10 个布尔值 = 0.122 秒
- array_unique:10 个布尔值= 0.274 秒
- foreach_bin:10 个布尔值= 0.095 秒
- in_array_bin:10 布尔值 = 0.073 秒
- array_sum:10布尔值= 0.074 秒
- array_filter:10布尔值= 0.118 秒
- 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:100 布尔值 = 0.153 秒
- foreach:100 布尔值 = 0.122 秒
- array_unique:100 布尔值 = 2.3451 秒
- foreach_bin:100布尔值= 0.094 秒
- in_array_bin:100 布尔值 = 0.074 秒
- array_sum: 100 bools= 0.126 秒
- array_filter:100布尔值= 0.228 秒
- 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
- in_array:1000 布尔值 = 0.154 秒
- foreach:1000 布尔值 = 0.149 秒
- array_unique: 1000 bools= 32.6659 秒 (!!)
- foreach_bin:1000布尔值= 0.075 秒
- in_array_bin:1000 布尔值 = 0.074 秒
- array_sum:1000 布尔值 = 0.8771 秒
- array_filter:1000 布尔值 = 1.4021 秒
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:10 个布尔值 = 0.152 秒
- foreach:10 个布尔值 = 0.342 秒
- array_unique:10 个布尔值= 0.269 秒
- foreach_bin:10 个布尔值= 0.074 秒
- in_array_bin:10 个布尔值 = 0.076 秒
- array_sum:10布尔值= 0.074 秒
- array_filter:10布尔值= 0.121 秒
- 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:100 布尔值 = 0.159 秒
- foreach:100 布尔值 = 2.8072 秒
- array_unique:100 布尔值 = 2.7702 秒
- foreach_bin:100布尔值= 0.074 秒
- in_array_bin:100 布尔值 = 0.09 秒
- array_sum:100布尔值= 0.118 秒
- array_filter:100布尔值= 0.248 秒
- 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
- in_array:1000 布尔值 = 0.312 秒
- foreach:1000 布尔值 = 27.5256 秒
- array_unique:1000 布尔值 = 42.1594 秒
- foreach_bin:1000布尔值= 0.074 秒
- in_array_bin:1000 布尔值 = 0.24 秒
- array_sum:1000布尔值= 0.555 秒
- array_filter:1000 布尔值 = 1.3601 秒
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.
然后,总结一下。这array_unique
条路清楚地抓住了尾巴,不要用于大型阵列或大量阵列!这种foreach
方式略有优势in_array
,但遗憾的是代码并不那么优雅。该array_sum
方法与对较小(<100)数组的“if true”检查相当。(我在 中挖掘了简单性array_sum($array) > 0
。)array_filter
方式有点落后于in_array
和foreach
。当只有最后的值不同,foreach
且array_unique
都拖动它坏。
Finally, the foreach
function for good humors. Very readable. The truth is out there!
最后,foreach
幽默的功能。很有可读性。真相就在那里!
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.
PS可以在上面使用'null',但这样读起来更有趣。在 Windows 上进行基准测试,因此微时间读数很粗。
回答by GolezTrol
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.
一个简单的循环就可以了。请注意,如果数组为空,则满足两个条件(全部为假和全部为真)。由于 else,您不会在结果中看到这一点,但您可以自行了解如何处理此问题。
// 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';
回答by Karoly Horvath
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.
注意:如果数组中有其他值,它们将根据 PHP 的可怕转换规则转换为布尔值。
回答by Marc B
If they're all array elements, with true/false values, then use array_flip():
如果它们都是数组元素,具有真/假值,则使用 array_flip():
$new = array_flip($array);
if (!isset($array[false]) && isset($array[true])) {
... there's no false values, and at least one true value
}
$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).
对于大型阵列,这可能会变得昂贵,因此您可能想尝试一下array_unique()
。你会得到一个最多有两个值(一个真,一个假)的数组。
Ok, so that wouldn't work. Simple shotgun approach:
好的,这样就行不通了。简单的霰弹枪方法:
if (in_array($yourarray, false, TRUE)) {
... at least one non-true value
}
回答by Sergey Onishchenko
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
回答by Manlio
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.
使用 for 循环。如果要检查所有变量是否为假,可以使用 for 循环:一旦找到真元素,就可以打破循环,否则变量都是假的。如果您想检查所有变量是否为真,您可以使用相同的方法。
回答by geekuality
// 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