How to check if a unique array is in PHP?

I want to know if an array has $arrduplicate elements.

+3
source share
3 answers

array_unique() must work:

if (count(array_unique($arr)) == count($arr)) 
  echo "Array does not contain duplicate elements"; 
else
  echo "Array contains duplicate elements";
+10
source

Check array_unique .

+3
source

I am not sure if there is a built-in function for this. But you could do

if (count($arr) == count(array_unique($arr))
{
    //array has no unique elements
}
0
source

Source: https://habr.com/ru/post/1751521/


All Articles