I've stumbled upon something weird and I don't understand why it works that way.
I have an array of numbers, they are all unique:
$array = [
98602142989816970,
98602142989816971,
98602142989816980,
98602142989816981,
98602142989816982,
98602142989816983,
98602142989820095,
98602142989820096,
98602142989822060,
98602142989822061,
];
var_dump($array);
array(10) {
[0]=>
int(98602142989816970)
[1]=>
int(98602142989816971)
[2]=>
int(98602142989816980)
[3]=>
int(98602142989816981)
[4]=>
int(98602142989816982)
[5]=>
int(98602142989816983)
[6]=>
int(98602142989820095)
[7]=>
int(98602142989820096)
[8]=>
int(98602142989822060)
[9]=>
int(98602142989822061)
}
If I do print_r(array_unique($array));
everything is fine, I get:
Array
(
[0] => 98602142989816970
[1] => 98602142989816971
[2] => 98602142989816980
[3] => 98602142989816981
[4] => 98602142989816982
[5] => 98602142989816983
[6] => 98602142989820095
[7] => 98602142989820096
[8] => 98602142989822060
[9] => 98602142989822061
)
But If I add SORT_NUMERIC
flag print_r(array_unique($array, SORT_NUMERIC));
I get:
Array
(
[0] => 98602142989816970
[6] => 98602142989820095
[8] => 98602142989822060
)
Why only those 3 numbers are returned?
update: I'm on 64-bit system.
For sort
functions I've manually shuffled some of the values because in original array they are already sorted.
If I do sort($array);
then response is as expected:
Array
(
[0] => 98602142989816970
[1] => 98602142989816971
[2] => 98602142989816980
[3] => 98602142989816981
[4] => 98602142989816982
[5] => 98602142989816983
[6] => 98602142989820095
[7] => 98602142989820096
[8] => 98602142989822060
[9] => 98602142989822061
)
But with sort($array, SORT_NUMERIC);
, they are sorted incorrectly:
Array
(
[0] => 98602142989816970
[1] => 98602142989816982
[2] => 98602142989816983
[3] => 98602142989816980
[4] => 98602142989816981
[5] => 98602142989816971
[6] => 98602142989820095
[7] => 98602142989820096
[8] => 98602142989822060
[9] => 98602142989822061
)
9.8602142989817E+16
,9.8602142989817E+16
, … already. If you are on a 64bit system, where these integers can be represented correctly to begin with, something probably goes wrong when SORT_NUMERIC comes into play - maybe that forces the use of 32bit / conversion to float internally again or something …sort()
instead of using the flag?