Search code examples
phpfloating-pointzeroarray-unique

array_unique ignores 0


I have an array of floats.

When I use array_unique, if I have a 0 value, it's left out of the result.

Is this correct, or is there a way around this? What's the proper syntax to make it include 0s.

Thanks in advance!


Solution

  • The array_unique function typecasts the elements of an array as strings by default before comparison.

    You might want to try:

    array_unique($array, SORT_NUMERIC);
    

    or

    array_unique($array, SORT_REGULAR);
    

    http://php.net/manual/en/function.array-unique.php