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

时间:2022-03-29 21:31:37

I want to know how I can write a method to convert a flat array of any size to a hierarchical tree (look at examples). Can anyone suggest a way to do this? Also, can this be done without using a large amount of resources?

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

Here is my example:

这是我的例子:

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
    )
);

Hierarchical Tree:

分层树:

$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 个解决方案

#1


1  

i propose object-oriented approach

我建议面向对象的方法

a. simple class representing node, with methods to addChild and transform self to array:

一个。表示节点的简单类,使用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
                         )
                );
        }
}

b. create flat array of TreeNodes from flat $categories array , and determinate roots of trees

湾从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']];
        }
}

c. add childs to TreeNodes

C。将子项添加到TreeNodes

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

d. convert trees to array starting from roots

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

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

#1


1  

i propose object-oriented approach

我建议面向对象的方法

a. simple class representing node, with methods to addChild and transform self to array:

一个。表示节点的简单类,使用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
                         )
                );
        }
}

b. create flat array of TreeNodes from flat $categories array , and determinate roots of trees

湾从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']];
        }
}

c. add childs to TreeNodes

C。将子项添加到TreeNodes

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

d. convert trees to array starting from roots

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

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