Two-dimensional array php sort of two-dimensional array of the implementation code in alphabetical order

Source: Internet
Author: User
Copy CodeThe code is as follows:


/**
* Sort an Two-dimension array by some level the use Array_multisort () function.
*
* Syssortarray ($Array, "Key1", "Sort_asc", "Sort_retular", "Key2" ...)
* @author Chunsheng Wang
* @param array $ArrayData the array to sort.
* @param string $KeyName 1 The first item to sort by.
* @param string $SortOrder 1 The order to sort by ("Sort_asc" | " Sort_desc ")
* @param string $SortType 1 the Sort type ("Sort_regular" | " Sort_numeric "|" Sort_string ")
* @return Array sorted array.
*/
function Syssortarray ($ArrayData, $KeyName 1, $SortOrder 1 = "Sort_asc", $SortType 1 = "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;
}
################# Example #################
$arr = Array (
Array
' Name ' = ' Learn ',
' Size ' = ' 1235 ',
' Type ' = ' jpe ',
' Time ' = ' 1921-11-13 ',
' Class ' = ' D ',
),
Array
' Name ' = ' Chinese Kung fu ',
' Size ' = ' 153 ',
' Type ' = ' jpe ',
' Time ' = ' 2005-11-13 ',
' Class ' = ' J ',
),
Array
' Name ' = ' programming ',
' Size ' = ' 35 ',
' Type ' = ' gif ',
' Time ' = ' 1997-11-13 ',
' Class ' = ' A ',
),
Array
' Name ' = ' Chinese Kung fu ',
' Size ' = ' 65 ',
' Type ' = ' jpe ',
' Time ' = ' 1925-02-13 ',
' Class ' = ' D ',
),
Array
' Name ' = ' Chinese Kung fu ',
' Size ' = ' 5 ',
' Type ' = ' icon ',
' Time ' = ' 1967-12-13 ',
' Class ' = ' C ',
),
);
Print_r ($arr);
Note: When sorting by number 153:65 Small
$temp = Syssortarray ($arr, "Class", "Sort_asc", "type", "Sort_desc", "size", "Sort_asc", "sort_string");
echo "




The above describes the two-dimensional array of PHP ordering two-dimensional array of the implementation of the code alphabetically, including the content of two-dimensional array, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.