doujiao3998 2012-08-31 10:49
浏览 26
已采纳

根据父级排序数组; 一维数组中的树

How can I sort an array with all children after their respective parents? I guess I'm trying to store a tree inside a one-dimensional array. I have tried to figure this out using usort, but I don't think it is the right tool for the job.

Example input array:

array (0 => array ( 'id' => '1', 'parent' => '0', ), 
1 => array ( 'id' => '2', 'parent' => '1', ), 
2 => array ( 'id' => '3', 'parent' => '0', ), 
3 => array ( 'id' => '5', 'parent' => '0', ), 
4 => array ( 'id' => '17', 'parent' => '3', ), 
5 => array ( 'id' => '31', 'parent' => '2', ), 
6 => array ( 'id' => '32', 'parent' => '2', ))

Example output:

Array sorted according to the description

  • 写回答

2条回答 默认 最新

  • dra11767 2012-08-31 10:53
    关注

    Start by building an actual tree, then flatten that tree:

    $array = array (0 => array ( 'id' => '1', 'parent' => '0', ),
                    1 => array ( 'id' => '2', 'parent' => '1', ),
                    2 => array ( 'id' => '3', 'parent' => '0', ),
                    3 => array ( 'id' => '5', 'parent' => '0', ),
                    4 => array ( 'id' => '17', 'parent' => '3', ),
                    5 => array ( 'id' => '31', 'parent' => '2', ),
                    6 => array ( 'id' => '32', 'parent' => '2', ));
    
    /* Building a tree. We also save a map of references to avoid                                
       searching the tree for nodes */
    
    //Helper to create nodes                                                                     
    $tree_node = function($id, $parent) {
      return array('id' => $id, 'parent' => $parent, 'children' => array());
    };
    
    $tree = $tree_node(0, null); //root node                                                     
    $map = array(0 => &$tree);
    foreach($array as $cur) {
      $id = (int) $cur['id'];
      $parentId = (int) $cur['parent'];
      $map[$id] =& $map[$parentId]['children'][];
      $map[$id] = $tree_node($id, $parentId);
    }
    
    //Now recursively flatten the tree:                                                          
    function flatter($node) {
      //Create an array element of the node                                            
      $array_element = array('id' => (string) $node['id'],
                             'parent' => (string) $node['parent']);
      //Add all children after me                                                                
      $result = array($array_element);
      foreach($node['children'] as $child) {
        $result = array_merge($result, flatter($child));
      }
      return $result;
    }
    
    $array = flatter($tree);
    array_shift($array); //Remove the root node, which was only added as a helper                
    
    print_r($array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型