How to check if all values ​​in an array are identical?

In PHP, how can I quickly determine if all values ​​in an array are identical?

+7
arrays php
source share
7 answers

You can use the test:

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

Alternatively, you can use the test:

 $arr === array_fill(0,count($arr),$arr[0]); 
+34
source share
 $results = array_unique($myArray); if(count($results) == 1){ // $myArray is all duplicates } 
+10
source share

You can also use this check:

 count(array_count_values($arr)) == 1 
+2
source share

Why not just iterate over this array?

0
source share
 $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"; } 
0
source share

Carry out a test run and check if all the results match:

 foreach ($array as $newarray){ echo $newarray. ''; } 
-one
source share

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

-one
source share

All Articles