如何将平面数组转换为分层树数组?

时间:2014-02-19 14:17:59

标签: php arrays

我想知道如何编写一个方法来将任意大小的平面数组转换为分层树(查看示例)。有谁能建议这样做的方法?另外,这可以在不使用大量资源的情况下完成吗?

以下是我的例子:

Flat Array:

$categories = array(
    array(
        'id' => 1,
        'sub' => 0
    ),
    array(
        'id' => 2,
        'sub' => 1
    ),
    array(
        'id' => 3,
        'sub' => 1
    ),
    array(
        'id' => 4,
        'sub' => 2
    ),
    array(
        'id' => 5,
        'sub' => 2
    ),
    array(
        'id' => 6,
        'sub' => 4
    ),
    array(
        'id' => 7,
        'sub' => 0
    )
);

分层树:

$categories = array(
    array(
        'id' => 1,
        'sub' => 0,
        'children' => array(
            array(
                'id' => 2,
                'sub' => 1,
                'children' => array(
                    array(
                        'id' => 4,
                        'sub' => 2,
                        'children' => array(
                            array(
                                'id' => 6,
                                'sub' => 4,
                                'children' => array()
                            )
                        )
                    ),
                    array(
                        'id' => 5,
                        'sub' => 2,
                        'children' => array()
                    )
                )
            ),
            array(
                'id' => 3,
                'sub' => 1,
                'children' => array()
            ),
        )
    ),
    array(
        'id' => 7,
        'sub' => 0,
                'children' => array()
    )
);

1 个答案:

答案 0 :(得分:1)

我建议面向对象的方法

一个。表示节点的简单类,使用addChild方法和将self转换为数组:

class TreeNode {
        public $id;
        public $parent;
        private $children;

        public function __construct($dataArray) {
                $this->id = $dataArray['id'];
                $this->parent = $dataArray['sub'];
                $this->children = array();
        }

        public function addChild(TreeNode $node) {
                $this->children[$node->id] = $node;
        }

        public function toArray() {
                return array(
                        'id'  => $this->id,
                        'sub' => $this->parent,
                        'children' => array_map(
                           function(TreeNode $element) { 
                             return $element->toArray();
                           }, $this->children
                         )
                );
        }
}

湾从flat $ categories数组创建TreeNodes的平面数组,并确定树的根

$nodes = array();
$rootNodes = array();
foreach($categories as $category) {
        $nodes[$category['id']] = new TreeNode($category);
        if ($category['sub'] == 0) {
                $rootNodes[] = $nodes[$category['id']];
        }
}

℃。将子项添加到TreeNodes

array_map(
  function(TreeNode $element) use($nodes){ 
    if(isset($nodes[$element->parent])) 
      $nodes[$element->parent]->addChild($element);
  }, $nodes
);

d。将树转换为从根开始的数组

print_r(array_map(
  function(TreeNode $element) { 
    return $element->toArray();
  }, $rootNodes)
);