检查数组中的所有值是否相同

时间:2021-10-05 07:55:37

I need to check if all values in an array equal the same thing.

我需要检查数组中的所有值是否相等。

For example:

例如:

$allValues = array(
    'true',
    'true',
    'true',
);

If every value in the array equals 'true' then I want to echo 'all true'. If any value in the array equals 'false' then I want to echo 'some false'

如果数组中的每个值都等于“true”,那么我要回显“all true”。如果数组中的任何值等于'false'那么我想要返回'some false'

Any idea on how I can do this?

你知道我该怎么做吗?

8 个解决方案

#1


91  

all values equal the test value

所有值都等于测试值

if (count(array_unique($allvalues)) === 1 && end($allvalues) === 'true') {


}

or just test for the existence of the thing you don't want.

或者只是测试你不想要的东西是否存在。

if (in_array('false', $allvalues, true)) {

}

Prefer the latter method if you're sure that there's only 2 possible values that could be in the array, as it's much more efficient. But if in doubt, a slow program is better than an incorrect program, so use the first method.

如果您确定数组中只有两个可能的值,那么最好使用后一种方法,因为这样更有效。但是如果有疑问,慢程序比不正确的程序要好,所以使用第一个方法。

If you can't use the second method, your array is very large, and the contents of the array is likely to have more than 1 value(especially if the value is likely to occur near the beginning of the array), it may be much faster to do the following:

如果您不能使用第二个方法,那么您的数组非常大,并且数组的内容可能有一个以上的值(特别是如果该值可能出现在数组的开头附近),那么执行以下操作可能要快得多:

function isHomogenous($arr) {
    $firstValue = current($arr);
    foreach ($arr as $val) {
        if ($firstValue !== $val) {
            return false;
        }
    }
    return true;
}

#2


10  

If your array contains actual booleans (or ints) instead of strings, you could use array_sum:

如果数组包含实际的布尔值(或ints)而不是字符串,可以使用array_sum:

$allvalues = array(TRUE, TRUE, TRUE);
if(array_sum($allvalues) == count($allvalues)) {
    echo 'all true';
} else {
    echo 'some false';
}

http://codepad.org/FIgomd9X

http://codepad.org/FIgomd9X

This works because TRUE will be evaluated as 1, and FALSE as 0.

这是成立的,因为TRUE值为1,FALSE值为0。

#3


6  

Also, you can condense goat's answer in the event it's not a binary:

同样,你也可以压缩山羊的答案,如果它不是二元的:

if (count(array_unique($allvalues)) === 1 && end($allvalues) === 'true') {
   // ...
}

to

if (array_unique($allvalues) === array('foobar')) { 
   // all values in array are "foobar"
}

#4


5  

Why not just compare count after calling array_unique()?

为什么不在调用array_unique()之后比较count呢?

To check if all elements in an array is the same, should be as simple as:

要检查数组中的所有元素是否相同,请简单如下:

$allValuesAreTheSame = (count(array_unique($allvalues)) === 1);

This should work regardless of the type of values in the array.

不管数组中的值是什么类型,这都应该有效。

#5


2  

You can compare min and max... not the fastest way ;p

你可以比较最小和最大值……不是最快的方法

$homogenous = ( min($array) === max($array) );

#6


0  

$alltrue = 1;
foreach($array as $item) {
    if($item!='true') { $alltrue = 0; }
}
if($alltrue) { echo("all true."); }
else { echo("some false."); }

Technically this doesn't test for "some false," it tests for "not all true." But it sounds like you're pretty sure that the only values you'll get are 'true' and 'false'.

从技术上讲,这不是测试“某些错误”,而是测试“并非全部正确”。但听起来你很确定你能得到的唯一值是“真”和“假”。

#7


0  

Another option:

另一个选择:

function same($arr) {
    return $arr === array_filter($arr, function ($element) use ($arr) {
        return ($element === $arr[0]);
    });
}

Usage:

用法:

same(array(true, true, true)); // => true

#8


-2  

$x = 0;
foreach ($allvalues as $a) {
   if ($a != $checkvalue) {
      $x = 1;
   }
}

//then check against $x
if ($x != 0) {
   //not all values are the same
}

#1


91  

all values equal the test value

所有值都等于测试值

if (count(array_unique($allvalues)) === 1 && end($allvalues) === 'true') {


}

or just test for the existence of the thing you don't want.

或者只是测试你不想要的东西是否存在。

if (in_array('false', $allvalues, true)) {

}

Prefer the latter method if you're sure that there's only 2 possible values that could be in the array, as it's much more efficient. But if in doubt, a slow program is better than an incorrect program, so use the first method.

如果您确定数组中只有两个可能的值,那么最好使用后一种方法,因为这样更有效。但是如果有疑问,慢程序比不正确的程序要好,所以使用第一个方法。

If you can't use the second method, your array is very large, and the contents of the array is likely to have more than 1 value(especially if the value is likely to occur near the beginning of the array), it may be much faster to do the following:

如果您不能使用第二个方法,那么您的数组非常大,并且数组的内容可能有一个以上的值(特别是如果该值可能出现在数组的开头附近),那么执行以下操作可能要快得多:

function isHomogenous($arr) {
    $firstValue = current($arr);
    foreach ($arr as $val) {
        if ($firstValue !== $val) {
            return false;
        }
    }
    return true;
}

#2


10  

If your array contains actual booleans (or ints) instead of strings, you could use array_sum:

如果数组包含实际的布尔值(或ints)而不是字符串,可以使用array_sum:

$allvalues = array(TRUE, TRUE, TRUE);
if(array_sum($allvalues) == count($allvalues)) {
    echo 'all true';
} else {
    echo 'some false';
}

http://codepad.org/FIgomd9X

http://codepad.org/FIgomd9X

This works because TRUE will be evaluated as 1, and FALSE as 0.

这是成立的,因为TRUE值为1,FALSE值为0。

#3


6  

Also, you can condense goat's answer in the event it's not a binary:

同样,你也可以压缩山羊的答案,如果它不是二元的:

if (count(array_unique($allvalues)) === 1 && end($allvalues) === 'true') {
   // ...
}

to

if (array_unique($allvalues) === array('foobar')) { 
   // all values in array are "foobar"
}

#4


5  

Why not just compare count after calling array_unique()?

为什么不在调用array_unique()之后比较count呢?

To check if all elements in an array is the same, should be as simple as:

要检查数组中的所有元素是否相同,请简单如下:

$allValuesAreTheSame = (count(array_unique($allvalues)) === 1);

This should work regardless of the type of values in the array.

不管数组中的值是什么类型,这都应该有效。

#5


2  

You can compare min and max... not the fastest way ;p

你可以比较最小和最大值……不是最快的方法

$homogenous = ( min($array) === max($array) );

#6


0  

$alltrue = 1;
foreach($array as $item) {
    if($item!='true') { $alltrue = 0; }
}
if($alltrue) { echo("all true."); }
else { echo("some false."); }

Technically this doesn't test for "some false," it tests for "not all true." But it sounds like you're pretty sure that the only values you'll get are 'true' and 'false'.

从技术上讲,这不是测试“某些错误”,而是测试“并非全部正确”。但听起来你很确定你能得到的唯一值是“真”和“假”。

#7


0  

Another option:

另一个选择:

function same($arr) {
    return $arr === array_filter($arr, function ($element) use ($arr) {
        return ($element === $arr[0]);
    });
}

Usage:

用法:

same(array(true, true, true)); // => true

#8


-2  

$x = 0;
foreach ($allvalues as $a) {
   if ($a != $checkvalue) {
      $x = 1;
   }
}

//then check against $x
if ($x != 0) {
   //not all values are the same
}