Sort two-dimensional arrays by array values using php
Multi-dimensional array sorting function code
- /**
- * Multi-dimensional array sorting
- * @ Param array $ array the array to be sorted
- * @ Param string $ sort by field of key
- * @ Param string $ order sorting method. 0 indicates descending order, and 1 indicates ascending order.
- */
- Function array_sort (array $ array, $ key, $ order = 1 ){
- $ Sort = [];
- // The correspondence between field values and key names is formed here
- Foreach ($ array as $ k => $ v ){
- $ Sort [$ v [$ key] = isset ($ sort [$ v [$ key])? Array_merge ($ sort [$ v [$ key], [$ k]): [$ k];
- }
- If ($ order = 1 & ksort ($ sort )){
- // Sort in ascending order
- } Elseif ($ order = 0 & krsort ($ sort )){
- } Else {
- Return false;
- }
- $ Rs = [];
- // Generate a new array based on the sorted relationship
- Foreach ($ sort as $ value ){
- Foreach ($ value as $ n ){
- $ Rs [] = $ array [$ n];
- }
- }
- Unset ($ sort, $ array );
- Return $ rs;
- }
|
Php