PHP 8.2: ksort(..., SORT_REGULAR) sort order changes

Version8.2
TypeChange

ksort function sorts a given array with arranging array keys in their ascending order.

There is a bug fix in the ksort implementation that causes the output to be different in PHP 8.2. This only affects when the ksort function is used with the optional parameter $flags = SORT_REGULAR. The default value for $flags parameter is SORT_REGULAR.

Prior to PHP 8.2, ksort it placed alphabetical keys prior to numeric keys. For example, when sorting an array with a, b, 1, and 2 as keys, the sorted order would be a, b, 1, 2 because alphabetical keys are placed prior to numeric keys. Since PHP 8.2, ksort function is made consistent with the other array sort functions, and places numeric keys prior to alphabetical keys.

 $array = ["a" => '', "1" => '', "b" => '', "2" => ''];
 ksort($array, SORT_REGULAR);  
 var_dump($array);

- ["a" => '', "b" => '', "1" => '', , "2" => ''];
+ ["1" => '', "2" => '', "a" => '', , "b" => ''];

Backwards Compatibility Impact

Although this change is a bug fix to make ksort behave similar to the rest of the array sort functions, this is a breaking change. Applications that need to restore the previous behavior may need to resolve to a the uksort function, which sorts an array using a user-provided comparison function.


Implementation