I am preparing upgrade to PHP 8.2 on a big application, and obviously going through changes between PHP 8.1 and 8.2. One of the BC breaking changes mentioned HERE is:
ksort() and krsort() now do numeric string comparison under SORT_REGULAR using the standard PHP 8 rules now.
I don't seem to understand what this means. Since ksort sorts by keys, I assumed BEFORE it would NOT sort something like this:
[
'-5' => 'minus five',
'4' => 'THIS SHOULD MOVE',
'1' => 'one',
'2' => 'two',
'100' => 'hundred',
];
ksort($arr, SORT_REGULAR);
var_dump($arr);
But I used https://onlinephp.io/ and the it works just fine on 7.x, 8.1 and 8.2.
I tried with SORT_REGULAR and without.
array(5) {
[-5]=>
string(10) "minus five"
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[4]=>
string(16) "THIS SHOULD MOVE"
[100]=>
string(7) "hundred"
}
Can someone explain to me what am I not understanding here?
This seems to be more about mixing alpha and numeric keys, so in your example this isn't an issue.
If you change the array to be
prior to 8.2, this gives (tested with Result for 8.1.13, 8.0.26, 7.4.33: on https://onlinephp.io/)
with the alpha being between the -ve and placed prior to the +ve numbers.
With 8.2, this instead will output
The article at PHP 8.2: ksort(..., SORT_REGULAR) sort order changes goes into this more with...