Multi-dimensional array sorting

Source: Internet
Author: User
Multi-dimensional array sorting has an array: PHPcode & lt ;? Php $ arr = array (1, 0), array (2, 0), array (3, 0), array (4, 0), array (5, 1), array (6, 1 ), array (7, 5), array (8, 2 ));? & Gt; multi-dimensional array sorting
There is an array:
PHP code
  
  


The result to be sorted is:
PHP code
  
  


That is, $ arr [$ I] [0] ==$ arr [$ j] [1]. then $ arr [$ j] is placed behind $ arr [$ I ].

Actually, it is an order of infinite categories. I try to process multiple cycles, but it is a little troublesome. I want to implement it using recursion, only $ arr [$ I] [0] = 1 is implemented. please help
There are only a few categories.

------ Solution --------------------
Write in this way
PHP code
$arr = array(array(1,0),array(2,0),array(3,0),array(4,0),array(5,1),array(6,1),array(7,5),array(8,2));print_r(foo($arr));function foo($ar, $p=0) {  $r = array();  foreach($ar as $v) {    if($v[1] == $p) {      $r[] = $v;      $r = array_merge($r, foo($ar, $v[0]));    }  }  return $r;}

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.