PHP two-dimensional array ordering (sense Shei brother)

Source: Internet
Author: User

Original by Brother Kingboard
/*                     _ooOoo_                    o8888888o                    88 " . " 88                     (| -_- |)                     O\  =  /O                 ____/'---' \____               . '   \\|     |//   ' .              /  \\| | |   :  | | |   \            /  _| | | | |  -:- | | | | | -  \            |   |  \\\  -  /// |   |             | \_|   ' \---/'   |   |             \  .-\__   '-'   ___/- .  /          ___ '.  . '   /--.--\   '.  . __       . ""   ' <   '. ___\_<|>_/___. '   > ' "" .      | | :   '- \ '.; ' \ _ /';. '/ -  ' &NBSP;:&NBSP;|&NBSP;|&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;\  \  '-.   \_ __\ /__ _/   .-'  /   / ====== '-.____ '-.___\_____/___.-' ____.-' ======                      ' =---= '   ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ the ^^            Buddha bless          never Bug */   array (Array (), array ()), $arr 1=array (Array (' Itemid ' =>  226, ' title ' =>  ' four near a hall for rent ', ' edittime ' => 1409387090),             array (' itemid ' => 311, ' title ' =>  ' four Chinese-western-Mandarin Garden residential building for lease ', ' edittime ' =  1409272174),             array (' itemid ' =  312, ' title ' =>  ' four east-west Mandarin Garden residential building rental 2 ', ' EditTime ' => 1409272074),          &nBsp;  array (' itemid ' => 313, ' title ' =>  ' four 3 ', ' EditTime '  =>  1409271174)          $arr 2=array (Array (' Itemid ' => 226, ' title ' = >  ' four near a hall for rent ', ' edittime ' => 1409287090),             array (' itemid ' => 314, ' title ' =>  ' four Chinese-western-Mandarin garden apartment rental ', ' edittime ' =>  1409172174),             array (' Itemid ' =>  310, ' title ' =>  ' four east-west Mandarin Garden residential building Rental 312 ', ' EditTime '  => 1309271174)          ), $b =mylist (Array_merge ($arr 1, $arr 2));p Rint_r ($b); Function mylist ($array) {      $arr =array ();     $list =array ()     foreach ($array  as   $v) {         $arr [$v [' Itemid ']]= $v [' EditTime '];          $list [$v [' Itemid ']]= $v;     }    arsort ($arr);      $newarr =array ()     foreach ($arr  as  $k = $v) {          $newarr []= $list [$k];    }    return   $newarr;}
I need to change
/** *  sorting a two-dimensional array by the specified key name  *  $array    passed array  *  $key        The specified key name  *  $type      The specified sort type   asc   ascending,desc  descending  */   function orderarray ($array  = array ()  , $key  =  ' id ',  $type  =   ' ASC ')   {         $arr          = array ();         $list         = array ();         foreach ($array  as $ V) {             $arr [$v [$key]]        =  $v [$key];              $list [$v [$key]]      =  $v;         }         if ($type  ==  ' ASC ') {         Asort ($arr);        }else{         arsort ($arr);        }                  $newarr      =array ();         foreach ($arr  as  $k  =>  $v) {              $newarr []       =$ list[$k];        }         return  $newarr;     }
 /**    *  bubble Sort, if the elements in the two-dimensional array are inconsistent, you can use the secondary method    add by   wangt    *   $arr    Arrays to sort     *   $key    specify the fields to sort     */    function interpretsort (  $arr  = array ()  , $key  =  ' order ') {         $count   = count ($arr);         if (  $count  <=  0 )  return false;        for (  $i  =  0;  $i  <  $count;  $i  ++ ) {                 for (  $j  =  $count  - 1;  $i  <  $j;  $j  --) {                          if (  $arr [$j] [$key] <  $arr [$j -1][$key] ) {                                  //exchange values for adjacent locations                                    $tmp  =  $arr [$j];                                   $arr [$j] =  $arr [$j -1];                                   $arr [$j -1]  =  $tmp;                         }                 }         }        return  $arr;}
Simple two-dimensional array sorted by specified field
echo  "<pre>";     $arr =array (         ' AAA ' =>array (             ' age ' =>32,              ' name ' = ' Zhang San '          ),         ' BBB ' =>array (              ' age ' =>12,              ' name ' = ' John Doe '         ),          ' CCC ' =>array (              ' age ' =>25,             ' name ' = > ' Zhao Liu '         )     );     Function paixu ($a, $b) {        return  $b [' age ']-$a [' age '];    }     uasort ($arr, ' Paixu ');     print_r ($arr);
the key to sort has a duplicate value
function Rorderarray ($array = [], $key = ', $order = ' asc ') {$sort = $order = = ' ASC '? ' Sort_asc ': ' Sort_desc '; $arrSort = Array ();   foreach ($array as $uniqid + $row) {foreach ($row as $k + = $value) {$arrSort [$k] [$uniqid] = $value;   }}array_multisort ($arrSort [$key], constant ($sort), $array); return $array;}

PHP two-dimensional array ordering (sense Shei brother)

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.