开发者

How to check whether a non-assoc array is unique in PHP?

开发者 https://www.devze.com 2023-01-04 19:04 出处:网络
I wan开发者_如何学Got to know whether the array $arr has duplicate elements.array_unique() should work:

I wan开发者_如何学Got to know whether the array $arr has duplicate elements.


array_unique() should work:

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


Check out array_unique.


I'm not sure there is a built in function for that. But you could do

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

精彩评论

暂无评论...
验证码 换一张
取 消