PHP Array sorting function

Source: Internet
Author: User
Tags array sort

$data []=array (' team ') ' = ' $item [' post_name '], ' name ' + $item [' name '], ' total ' = $total, ' total_point ' =>$ Total_pt, ' total_hd ' = $total _hd, ' Chousale ' and $ChouSale, ' chousale_pt ' = $ChouSale _pt, ' CHOUSALE_HD ' =>$ CHOUSALE_HD,); $data = Syssortarray ($data, ' total_point ', ' sort_desc ', ' sort_numeric '); 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;}

  Sort using PHP's own Array_multisort function

$data = Array ();    $data [] = Array (' volume ' = +, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 1);    $data [] = Array (' volume ' = =, ' edition ' = 6);    $data [] = Array (' volume ' = = 98, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 6);    $data [] = Array (' volume ' = +, ' edition ' = 7);    Get the list of columns    foreach ($data as $key + = $row)    {        $volume [$key]  = $row [' volume '];        $edition [$key] = $row [' edition '];    }    Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);    Print_r ($data);

  Custom Function Sort 1

$data = Array ();    $data [] = Array (' volume ' = +, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 1);    $data [] = Array (' volume ' = =, ' edition ' = 6);    $data [] = Array (' volume ' = = 98, ' edition ' = 2);    $data [] = Array (' volume ' = +, ' edition ' = 6);    $data [] = Array (' volume ' = +, ' edition ' = 7);        Get the list of columns foreach ($data as $key + = $row) {$volume [$key] = $row [' volume '];    $edition [$key] = $row [' Edition '];    } $ret = ArraySort ($data, ' volume ', ' desc ');    Print_r ($ret);     /** * @desc arraysort php Two-dimensional array sort sorted by the specified key * @param array $arr will be sorted * @param string $keys Specify the sorted key * @param string $type sort type ASC | DESC * @return Array */function ArraySort ($arr, $keys, $type = ' asc ') {$keysvalue = $new _array = array        ();        foreach ($arr as $k = + $v) {$keysvalue [$k] = $v [$keys]; } $type = = ' ASC '? Asort ($keysvalue): Arsort ($KEYSVAlue);        Reset ($keysvalue);        foreach ($keysvalue as $k = + $v) {$new _array[$k] = $arr [$k];    } return $new _array; }

  

PHP Array sorting function

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.