One-dimensional array sorting
1. sort, rsort sorting (r (reverse flip represented, ie in descending order)), by the sorting key.
<? PHP $ ARR = Array (10,2,4,1,5,8,7,9 ); // equivalent to $ arr = array ( '0' => 10, '1' => 2, ' 2 '=> 4,' 3 '=> 1,' 4 '=> 5' 5 '=> 8,' 6 '=> 7,' 7 '=> 9); // default sort key value ascending order, the key names are re-specified, for example 10 original key name is 0, now. 7 Sort ( $ ARR ); var_dump ( $ ARR ); / * Array (size =. 8) 0 => int. 1 . 1 => int 2 2 => int. 4 . 3 => int. 5 . 4 => int. 7 . 5 => int. 8 . 6 => int. 9 . 7 => 10 int * / ?>
? < PHP $ arr = Array (10,2,4,1,5,8,7,9 ); // default sort key values in descending order, were the keys from the new provisions, ibid rsort ( $ arr ); var_dump ( $ ARR ); / * Array (size =. 8) 0 => int 10 . 1 => int. 9 2 => int. 8 . 3 => int. 7 . 4 => int. 5 . 5 => int. 4 . 6 => int 2 . 7 = > int 1 * / ?>
2. ksort, krsort sorting, sorting by key name.
? < PHP $ ARR = Array (10,2,4,1,5,8,7,9 ); // key keys in ascending order ksort ( $ ARR ); var_dump ( $ ARR ); / * Array (size = . 8) 0 => 10 int . 1 => 2 int 2 => int. 4 . 3 => int. 1 . 4 => int. 5 . 5 => int. 8 . 6 => int. 7 . 7 => int. 9 * / ?>
? < PHP $ arr = Array (10,2,4,1,5,8,7,9 ); // button key name in descending order krsort ( $ arr ); var_dump ( $ arr ); / * Array (size = . 8) . 7 => int. 9 . 6 => int. 7 . 5 => int. 8 . 4 => int. 5 . 3 => int. 1 2 => int. 4 . 1 => 2 int 0 => 10 int * / ?>
3. asort, arsort sorting, sorting by keys.
? < PHP $ arr = Array (10,2,4,1,5,8,7,9 ); // button keys in ascending order, and sort different is that keys will not redefine asort ( $ arr ) ; var_dump ( $ ARR ); / * Array (size =. 8) . 3 => int. 1 . 1 => int 2 2 => int. 4 . 4 => int. 5 . 6 => int. 7 . 5 => int. 8 . 7 => int. 9 = 0> 10 int * / ?>
? < PHP $ arr = Array (10,2,4,1,5,8,7,9 ); // button keys in descending order, and rsort different is that keys will not redefine arsort ( $ arr ) ; var_dump ( $ ARR ); / * Array (size =. 8) 0 => int 10 . 7 => int. 9 . 5 => int. 8 . 6 => int. 7 . 4 => int. 5 2 => int. 4 . 1 => int 2 = 3> 1 int * / ?>
Two-dimensional array is sorted according to a key
//二维数组排序 function sortArr($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){ $key_arrays =array(); if(is_array($arrays)){ foreach ($arrays as $array){ if(is_array($array)){ $key_arrays[] = $array[$sort_key]; }else{ Return to false ; } } } the else { return to false ; } array_multisort ( $ key_arrays , $ the sort_order , $ SORT_TYPE , $ Arrays ); return $ Arrays ; } // $ Data represent two-dimensional array, for example, read from the data structure multi-line table structure, 'date' field in each row represents a inside, this field by sorting // SORT_DESC expressed in descending order, provided a comparative SORT_STRING 'date' field as a character string for $ newData = $ the this -> sortArr ( $ Data , 'DATE', SORT_DESC, SORT_STRING);
Two-dimensional array sorted by multiple keys
//二维数组多键值排序 function my_sort($arrays,$sort_key,$sort_key1,$sort_order=SORT_ASC,$sort_order1=SORT_DESC){ $key_arrays = $key_arrays1 = array(); if(is_array($arrays)){ foreach ($arrays as $array){ if(is_array($array)){ $key_arrays[] = $array[$ sort_key ]; $ key_arrays1 [] = $ Array [ $ sort_key1 ]; } the else { return to false ; } } } the else { return to false ; } array_multisort ( $ key_arrays , $ the sort_order , $ key_arrays1 , $ sort_order1 , $ Arrays ); return Arrays $ ; } // $ data represent two-dimensional array, for example, read from the data structure of a multi-row table structure // 'DATE' represents a field of each line inside of sorting through this field, and then by 'time' field ordering // SORT_DESC, SORT_ASC corresponding 'date', sort 'time' two fields // effects similar database order by data desc, time asc. newData $ = $ the this -> sortArr ( $ Data , 'DATE', 'Time', SORT_DESC, SORT_ASC);
From the micro-channel public number: Programming Society
Advanced programmers daily book, please pay attention!