Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ksort() now uses regular comparison

ksort() used a different sorting method to sort the keys than to sort the values. Since PHP 8.2, it uses the same method than sort() does on values. This means some values may have a different position.

This applies to krsort() too. It may apply to uksort(), depending on the code of the custom comparison function.

PHP code

<?php

$array = [ 0, '-f' => 1, 'f' => 2];

ksort($array);

print_r($array);

?>

Before

Array
(
    [0] => 0
    [-f] => 1
    [f] => 2
)

After

Array
(
    [-f] => 1
    [0] => 0
    [f] => 2
)

PHP version change

This behavior changed in 8.2.

© 2023-2026, Damien Seguy - Exakat