PHP Array custom sort: Php two-dimensional array custom sort

Source: Internet
Author: User
Tags foreach array count key sort

<?php
Custom sort for two-dimensional arrays
function Array_sort ($arr, $keys, $type) {
$keysvalue =array ();
$i = 0;
foreach ($arr as $key => $val) {
$val [$keys] = Str_replace ("-", "", $val [$keys]);
$val [$keys] = Str_replace ("", "", $val [$keys]);
$val [$keys] = Str_replace (":", "", $val [$keys]);
$keysvalue [] = $val [$keys];
}
Asort ($keysvalue); Key value Ordering
Reset ($keysvalue); The pointer points back to the array first
foreach ($keysvalue as $key => $vals) {
$keysort [] = $key;
}
$new _array = Array ();
if ($type!= "ASC") {
For ($ii =count ($keysort)-1; $ii >=0; $ii-) {
$new _array[] = $arr [$keysort [$ii]];
}
}else{
for ($ii =0; $ii <count ($keysort); $ii + +) {
$new _array[] = $arr [$keysort [$ii]];
}
}
return $new _array;
}
$arr = Array (
Array
' Name ' => ' learning ',
' Size ' => ' 1235 ',
' Type ' => ' JPE ',
' Time ' => ' 1921-11-13 ',
' class ' => ' DD ',
),
Array
' Name ' => ' Chinese Kung Fu ',
' Size ' => ' 153 ',
' Type ' => ' JPE ',
' Time ' => ' 2005-11-13 ',
' Class ' => ' JJ ',
),
Array
' Name ' => ' programming ',
' Size ' => ' 35 ',
' Type ' => ' gif ',
' Time ' => ' 1997-11-13 ',
' class ' => ' DD ',
),
Array
' Name ' => ' Chinese Kung Fu ',
' Size ' => ' 65 ',
' Type ' => ' JPE ',
' Time ' => ' 1925-02-13 ',
' class ' => ' yy ',
),
);
Echo ' <pre> ';
Print_r (Array_sort ($arr, ' size ', ' ASC '));
function Syssortarray ($ArrayData, $KeyName 1, $SortOrder 1 = "Sort_asc", $SortType 1 = "Sort_regular")
{
if (!is_array ($ArrayData))
{
return $ArrayData;
}
Get args number.
$ArgCount = Func_num_args ();
Get keys to sort by and put them to sortrule array.
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;
}
?> This article links http://www.cxybl.com/html/wlbc/Php/20130326/37392.html

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.