Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all values in array are identical?

Tags:

arrays

php

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

like image 222
davidP Avatar asked Mar 31 '10 15:03

davidP


People also ask

How can you tell if an array is identical?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

How do you compare all elements in an array?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

How do you check if all values in an array are equal Javascript?

Javascript Useful Snippets — allEqual() In order to check whether every value of your records/array is equal to each other or not, you can use this function. allEqual() function returns true if the all records of a collection are equal and false otherwise.


2 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]);
like image 82
codaddict Avatar answered Oct 04 '22 22:10

codaddict


$results = array_unique($myArray);
if(count($results) == 1){
   // $myArray is all duplicates
}
like image 44
brettkelly Avatar answered Oct 04 '22 21:10

brettkelly