uksort
Use user-defined comparison functions to sort key names in an array
The uksort()
function sorts the array key names through user-defined comparison functions.
Tip: Please use the uasort()
function to sort the array by key value through a user-defined comparison function, which uses a user-defined comparison function to sort.
Use a user-defined comparison function to sort the key names of elements in the array $arr:
<?php function my_sort ( $a , $b ) { if ( $a == $b ) return 0 ; return ( $a < $b ) ? - 1 : 1 ; } $arr = array ( "a" => 4 , "b" => 2 , "c" => 8 , d => "6" ) ; uksort ( $arr , "my_sort" ) ; ?>
Try it yourself