Copy codeThe Code is as follows:
<? Php
/**
* Sort an two-dimension array by some level two items use array_multisort () function.
*
* SysSortArray ($ Array, "Key1", "SORT_ASC", "SORT_RETULAR", "Key2 "......)
* @ Author Chunsheng Wang <wwccss@263.net>
* @ Param array $ ArrayData the array to sort.
* @ Param string $ KeyName1 the first item to sort.
* @ Param string $ SortOrder1 the order to sort by ("SORT_ASC" | "SORT_DESC ")
* @ Param string $ SortType1 the sort type ("SORT_REGULAR" | "SORT_NUMERIC" | "SORT_STRING ")
* @ Return array sorted array.
*/
Function sysSortArray ($ ArrayData, $ KeyName1, $ SortOrder1 = "SORT_ASC", $ SortType1 = "SORT_REGULAR ")
{
If (! Is_array ($ ArrayData ))
{
Return $ ArrayData;
}
$ ArgCount = func_num_args ();
For ($ I = 1; $ I <$ ArgCount; $ I ++)
{
$ Arg = func_get_arg ($ I );
If (! Eregi ("SORT", $ Arg ))
{
$ KeyNameList [] = $ Arg;
$ SortRule [] = '$'. $ Arg;
}
Else
{
$ SortRule [] = $ Arg;
}
}
Foreach ($ ArrayData AS $ Key => $ Info)
{
Foreach ($ KeyNameList AS $ KeyName)
{
$ {$ KeyName} [$ Key] = $ Info [$ KeyName];
}
}
$ EvalString = 'array _ multisort ('. join (",", $ SortRule).', $ ArrayData );';
Eval ($ EvalString );
Return $ ArrayData;
}
#################
$ Arr = array (
Array (
'Name' => 'learn ',
'SIZE' => '123 ',
'Type' => 'jpe ',
'Time' => '2017-11-13 ',
'Class' => 'D ',
),
Array (
'Name' => 'Kung Fu China ',
'SIZE' => '123 ',
'Type' => 'jpe ',
'Time' => '2017-11-13 ',
'Class' => 'J ',
),
Array (
'Name' => 'programmatically ',
'SIZE' => '35 ',
'Type' => 'gif ',
'Time' => '2017-11-13 ',
'Class' => 'A ',
),
Array (
'Name' => 'Kung Fu China ',
'SIZE' => '65 ',
'Type' => 'jpe ',
'Time' => '2017-02-13 ',
'Class' => 'D ',
),
Array (
'Name' => 'Kung Fu China ',
'SIZE' => '5 ',
'Type' => 'icon ',
'Time' => '2017-12-13 ',
'Class' => 'C ',
),
);
Print_r ($ arr );
// Note: When sorting by number, 153 is smaller than 65.
$ Temp = sysSortArray ($ arr, "class", "SORT_ASC", "type", "SORT_DESC", "size", "SORT_ASC", "SORT_STRING ");
Echo "<pre> ";
Print_r ($ temp );
?>