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

时间:2021-12-26 00:36:56

In PHP how can I quickly tell if all values in array are identical?

在PHP中,如何快速判断数组中的所有值是否相同?

7 个解决方案

#1


35  

You can use the test:

你可以使用测试:

count(array_unique($arr)) == 1;

Alternatively you can use the test:

或者你可以使用测试:

$arr === array_fill(0,count($arr),$arr[0]);

#2


11  

$results = array_unique($myArray);
if(count($results) == 1){
   // $myArray is all duplicates
}

#3


2  

You could also use this check:

您也可以使用此检查:

count(array_count_values($arr)) == 1

#4


0  

Why not to just loop over this array?

为什么不循环遍历这个数组呢?

#5


0  

$myArray = array('1','1','1');
$results = array_unique($myArray);
if(count($results) == 1)
{
    echo"all value is duplicates";
}
else
{
    echo"all value is not duplicates";
}

#6


-1  

You can check for count(array_intersect($arr1, $arr2)) == 0

您可以检查count(array_intersect($ arr1,$ arr2))== 0

#7


-1  

Do a test run and see if all results are the same:

进行测试并查看所有结果是否相同:

foreach ($array as $newarray){
    echo $newarray. '';
}

#1


35  

You can use the test:

你可以使用测试:

count(array_unique($arr)) == 1;

Alternatively you can use the test:

或者你可以使用测试:

$arr === array_fill(0,count($arr),$arr[0]);

#2


11  

$results = array_unique($myArray);
if(count($results) == 1){
   // $myArray is all duplicates
}

#3


2  

You could also use this check:

您也可以使用此检查:

count(array_count_values($arr)) == 1

#4


0  

Why not to just loop over this array?

为什么不循环遍历这个数组呢?

#5


0  

$myArray = array('1','1','1');
$results = array_unique($myArray);
if(count($results) == 1)
{
    echo"all value is duplicates";
}
else
{
    echo"all value is not duplicates";
}

#6


-1  

You can check for count(array_intersect($arr1, $arr2)) == 0

您可以检查count(array_intersect($ arr1,$ arr2))== 0

#7


-1  

Do a test run and see if all results are the same:

进行测试并查看所有结果是否相同:

foreach ($array as $newarray){
    echo $newarray. '';
}