Php two-dimensional array sorting

Source: Internet
Author: User
Php 2D array sorting problem help sort existing array PHPcode $ a = array (0 = & gt; array (3 = & gt; 9), 1 = & gt; array (2 = & gt; 9), 2 = & gt; array (6 = & gt; 5), php two-dimensional array sorting problem to help sort
Existing array
PHP code
  $a=array(         0=>array(3=>9),    1=>array(2=>9),    2=>array(6=>5),    3=>array(1=>15),    4=>array(2=>4),);

The sorting is as follows:
PHP code
  $b=array(    0=>array(1=>15),    1=>array(3=>9),    2=>array(2=>9),    3=>array(6=>5),    4=>array(2=>4),    );


Wish you all the best!

------ Solution --------------------
PHP code
$ A = array (0 => array (3 => 9), 1 => array (2 => 9), 2 => array (6 => 5 ), 3 => array (1 => 15), 4 => array (2 => 4),); foreach ($ a as $ v) {$ r [] = current ($ v); $ k [] = key ($ v);} array_multisort ($ r, SORT_DESC, $ k, SORT_DESC, $ ); print_r ($ );
------ Solution --------------------
Discussion
PHP code

$ A = array (
0 => array (3 => 9 ),
1 => array (2 => 9 ),
2 => array (6 => 5 ),
3 => array (1 => 15 ),
4 => array (2 => 4 ),
);

Foreach ($ a as $ v ){
$ R [] = current ($ v );
$ K [] = key ($ v );
}
......

------ Solution --------------------
Taught
------ Solution --------------------
My blog has exactly one. The first floor is Pediatrics.
------ Solution --------------------
No dressing change
PHP code
……usort($a, create_function('$m, $n', 'return current($m)==current($n) ? (key($m)==key($n) ? 0 : key($m)
    
      Array        (            [1] => 15        )    [1] => Array        (            [3] => 9        )    [2] => Array        (            [2] => 9        )    [3] => Array        (            [6] => 5        )    [4] => Array        (            [2] => 4        ))/**/
    

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.