Array_multisort--Sort multiple arrays or multidimensional arrays [reference: Secure.php.net]
Description
bool array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] )
array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.
Parameters
- ==array1== the array to sort
- ==array1_sort_order== the order in which the array parameter is to be arranged. Sort_asc are sorted in ascending order andSort_desc in descending order. This parameter can be interchanged with ==array_sort_flags==, or it can be deleted, and the default is SORT_ASC.
- ==array1_sort_flags== SET options for the array parameter:
Sort Type flag:
Sort_regular-Compare items by usual method (do not modify type)
Sort_numeric-Compare by number size
Sort_string-by string comparison
Sort_locale_string-based on the current localization settings, by string comparison. It uses locale information and can modify this information through setlocale ().
Sort_natural-"natural sort" with strings, similar to Natsort ()
Sort_flag_case-You can combine (bitwise OR OR) sort_string or sort_natural case-insensitive to sort strings.
Parameters can be exchanged or omitted from Array1_sort_order, which is sort_regular by default.
return value
Returns true on success, or false on failure
Instance:
$nums = array(1,4,5,2,9);$str = array("a","g","i","z","k");array_multisort($nums , SORT_DESC , $str);print_r($nums);print_r($str);$arr = array( array("spid"=>"1","time"=>100), array("spid"=>"1","time"=>105), array("spid"=>"2","time"=>104), array("spid"=>"1","time"=>102), array("spid"=>"2","time"=>101),);$spids = array();$times = array();foreach ($arr as $key => $value) { $spids[] = $value[‘spid‘]; $times[] = $value[‘time‘];}//array_multisort($times,SORT_DESC ,$spids,SORT_ASC , $arr );//先按照spids列降序,再按照times列升序array_multisort($spids,SORT_ASC ,$times,SORT_DESC , $arr );//array_multisort($spids,SORT_DESC , $arr );print_r($arr);
Output:
9 5 4) 2 1
K I g Z a
["1": 105, "1": 102, "1": 100, "2": 104, "2": 101]
Array sort Array_multisort