PHP Two-dimensional array custom sorting

Source: Internet
Author: User
<?php//a two-dimensional array custom sort 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 sort reset ($keysvalue);    The pointer re-points 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 (' 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 ' = ' + ', ' type ' = ' gif ', ' time ' and ' 1997-11                -13 ', ' class ' = ' dd ', ', ' Array (' name ' = ' Chinese Kung Fu '),     ' Size ' = ' + ', ' type ' = ' jpe ', ' time ' = ' 1925-02-13 ', ' class ' = ' yy ', '), Echo ' <pre> ';p rint_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) {${$Key        name}[$Key] = $Info [$KeyName];    }} $EvalString = ' Array_multisort ('. Join (",", $SortRule). ', $ArrayData);    eval ($EvalString); return $ArrayData;}? >
  • 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.