Use a user-defined comparison function to sort the elements in the array $arr by key value:
<?phpfunction my_sort($a,$b){if ($a==$b) return 0;return ($a<$b)?-1:1;}$arr=array("a"=>4 ,"b"=>2,"c"=>8,d=>"6");uasort($arr,"my_sort");?>uasort() sorts an array by key value using a user-defined comparison function.
Tip: Please use the uksort() function to sort the array by key name, which uses a user-defined comparison function to sort.
uasort( array,myfunction );
parameter | describe |
---|---|
array | Required. Specifies the array to be sorted. |
myfunction | Optional. A string that defines a callable comparison function. If the first parameter <, =, > the second parameter, the corresponding comparison function must return an integer <, =, > 0. |
Return value: | Returns TRUE if successful and FALSE if failed. |
---|---|
PHP version: | 4+ |