How to check whether a non-assoc array is unique in PHP?
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
}
精彩评论