PHP multidimensional Array ordering Array_multisort () ____php

Source: Internet
Author: User
Tags array sort
Array_multisort (Array_column ($tpldata, ' Time_stamp '), Sort_desc, $tpldata); Sort_desc Descending; Sort_asc Ascending time_stamp sort Field
multidimensional array sorting array_multisort () using PHP to sort with array_multisort functions <?php     $data = Array ();     $data [] = Array (' volume ' =>, ' Edition ' => 2);     $data [] = Array (' volume ' =>, ' edition ' => 1);     $data [] = Array (' volume ' =>, ' Edition ' => 6);     $data [] = Array (' volume ' =>, ' Edition ' => 2);     $data [] = Array (' volume ' =>, ' Edition ' => 6);     $data [] = Array (' volume ' =>, ' edition ' => 7);    //Get 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);?> output Result: Copy code   Code as follows: Array (    [0] => array   &nb sp;   &nbsp (            [Volume] =>              [Edition] => 2        )      [1] => Array         (             [Volume] =>             [ Edition] => 1             [2] => Array          (            [Volume] => 86             [Edition] => 6         )     [3] => Array         (             [Volume] =>             [Edition] => 6        )      [4] => Array         (             [Volume] =>             [ Edition] => 2             [5] => Array          (            [volume] => 67             [Edition] => 7         ) about Array_multisort official documentation also has a more detailed description: http://www.php.net/manual/zh/function.array-multisort.php Two, custom function sort 1Copy code code as follows:
<?php     $data = Array ();     $data [] = Array (' volume ' =>, ' Edition ' => 2);     $data [] = Array (' volume ' =>, ' edition ' => 1);     $data [] = Array (' volume ' =>, ' Edition ' => 6);     $data [] = Array (' volume ' =>, ' Edition ' => 2);     $data [] = Array (' volume ' =>, ' Edition ' => 6);     $data [] = Array (' volume ' =>, ' edition ' => 7);    //Get 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 According to the specified key array      * Param ARRAY $arr the array that will be sorted      * @param string $keys Specifies the key      * @param string $type sort type ASC | Desc      * @return array     /    function ArraySort ($arr, $ Keys, $type = ' asc ') {        $keysvalue = $new _array = Array (); &NBSP;&NBSP;&NBSP;&N bsp;    foreach ($arr as $k => $v) {             $keysvalue [$k] = $v [$keys];        }         $type = = ' ASC '? Asort ($keysvalue): Arsort ($keysvalue);         Reset ($keysvalue);         foreach ($keysvalue as $k => $v) {      & nbsp;    $new _array[$k] = $arr [$k];        }         return $new _array;    }?>
Output Result: Copy code   code as follows: Array (    [3] => Array         ( &nbs p;          [Volume] =>              [Edition] => 2        )     [4] = > Array         (             [Volume] =>             [Edition] => 6 & nbsp;      )     [1] => Array          (            [volume] =>              [Edition] => 1        )     [2] => Array         (            [Volume] =>              [Edition] => 6        )      [5] => Array         (             [Volume] =>             [Edition] = > 7        )     [0] => Array      & nbsp;  (            [volume] =>              [Edition] => 2         ) One of the differences between this custom function and the system function is that the custom function only supports sorting for a key, and it needs to be executed multiple times to support the ordering of multiple key keys; and system function Array_multisort can be one-time to multiple keys and can specify more than one collation, system function is quite powerful, recommend the use of system functions, BI

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.