English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The uasort() function sorts the values in the array using a user-defined comparison function and maintains the index association
uasort ( $array, $cmp_function )
This function sorts the array to keep the array index in correlation with the associated array elements. It is mainly used for sorting associative arrays where the actual element order is very important. The comparison function is user-defined.
Serial Number | Parameters and Description |
---|---|
1 | array(Required) It specifies an array. |
2 | cmp_function(Required) If the function is defined, it is used to compare the values and sort them. The function must return-1、0 or1,this method can work normally. It should be written to accept two parameters for comparison, and its working method should be similar to the following−
|
Returns TRUE on success, FALSE on failure.
<?php function cmp_function($a, $b) { if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } $input = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" ); uasort($input, "cmp_function"); print_r($input); ?>Test and see‹/›
Output Result:
Array ( [a] => orange [d] => lemon [b] => banana )