Php two-dimensional array sorting function, which is a natural sorting by default, that is, sort sorting. You can specify multiple methods to sort values by values in a two-dimensional array. For more information, see the program annotations below.
Copy codeThe Code is as follows:
/**
* @ Function two-dimensional array natural sorting
* @ Author www.phpernote.com
* @ Param array $ array the array to be sorted (two-dimensional)
* @ Param string key to be sorted by which key
* @ Param string order sorting method (SORT_ASC, SORT_DESC)
* @ Param string type sorting method (SORT_REGULAR, SORT_NUMERIC, SORT_STRING)
* @ Return array sorted array.
*/
Function phpSortArray ($ array, $ key, $ order = "SORT_ASC", $ type = "SORT_REGULAR "){
If (! Is_array ($ array) | empty ($ array )){
Return $ array;
}
$ ArgCount = func_num_args (); // returns the number of parameters passed to the currently defined function.
For ($ I = 1; $ I <$ ArgCount; $ I ++ ){
$ Arg = func_get_arg ($ I );
If (! Eregi ("SORT", $ Arg )){
$ KeyNameList [] = $ Arg;
$ SortRule [] = '$'. $ Arg;
} Else {
$ SortRule [] = $ Arg;
}
}
Foreach ($ array AS $ Key => $ Info ){
Foreach ($ KeyNameList as $ KeyName ){
$ {$ KeyName} [$ Key] = $ Info [$ KeyName];
}
}
$ EvalString = 'array _ multisort ('. join (",", $ SortRule).', $ array );';
Eval ($ EvalString );
Return $ array;
}