Home  >  Article  >  Backend Development  >  多维数组排序有关问题

多维数组排序有关问题

WBOY
WBOYOriginal
2016-06-13 10:16:31767browse

多维数组排序问题
有一数组:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $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)); ?>


想要排序的结果为:
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $arr = array(array(1,0),array(5,1),array(7,5),array(6,1),array(2,0),array(8,2),array(3,0),array(4,0)); ?>


即:$arr[$i][0] == $arr[$j][1] 那么$arr[$j]放在$arr[$i]后面

其实就是一个无限分类的一个排序,我试过多个循环来处理,不过有点麻烦,想用递归来实现,只实现了$arr[$i][0]=1的情况,请大家帮忙
分类比较少,就这么多了

------解决方案--------------------
这样写
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;}<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn