Code for sorting two-dimensional arrays in PHP by letter

Source: Internet
Author: User

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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.