EasyTree 一个生成树型数据结构最简单的方式

前言

最近在做公司的项目开发,类似CMS的一个系统,尤其是这个分类,是让我特别头疼的,各种不方便。其实就是一个树型结构的数组(PS:刚开始做,分类的数据都是写死的),在开发完任务以后,忙里偷闲自己写了一个library,欢迎各位大佬指出不合适的地方,小弟虚心改进 :smile:

安装

$ composer require lambert/tree-shape

今早刚发布,还热乎的,:joy:

使用

use Lambert\TreeShape\Manager;

$category = [
    ['id' => 1, 'name' => '节点1', 'pid' => 0],
    ['id' => 2, 'name' => '节点2', 'pid' => 0],
    ['id' => 3, 'name' => '节点3', 'pid' => 1],
    ['id' => 4, 'name' => '节点4', 'pid' => 1],
];

// Start build
$manager = Manager::buildTree($category, 'pid');
$tree = $manager->all();

生成后结构为:

Array
(
    [1] => Lambert\TreeShape\Tree Object
        (
            [childrenIds:protected] => Array
                (
                    [0] => 3
                    [1] => 4
                )

            [recursionChildrenIds:protected] =>
            [currentNode:protected] => Lambert\TreeShape\Node Object
                (
                    [data:protected] => Array
                        (
                            [id] => 1
                            [name] => 节点1
                            [pid] => 0
                        )

                )

            [leftTree:protected] =>
            [rightTree:protected] => Array
                (
                    [3] => Lambert\TreeShape\Tree Object
                        (
                            [childrenIds:protected] =>
                            [recursionChildrenIds:protected] =>
                            [currentNode:protected] => Lambert\TreeShape\Node Object
                                (
                                    [data:protected] => Array
                                        (
                                            [id] => 3
                                            [name] => 节点3
                                            [pid] => 1
                                        )

                                )

                            [leftTree:protected] => Lambert\TreeShape\Tree Object
 *RECURSION*
                            [rightTree:protected] =>
                        )

                    [4] => Lambert\TreeShape\Tree Object
                        (
                            [childrenIds:protected] =>
                            [recursionChildrenIds:protected] =>
                            [currentNode:protected] => Lambert\TreeShape\Node Object
                                (
                                    [data:protected] => Array
                                        (
                                            [id] => 4
                                            [name] => 节点4
                                            [pid] => 1
                                        )

                                )

                            [leftTree:protected] => Lambert\TreeShape\Tree Object
 *RECURSION*
                            [rightTree:protected] =>
                        )

                )

        )

    [2] => Lambert\TreeShape\Tree Object
        (
            [childrenIds:protected] =>
            [recursionChildrenIds:protected] =>
            [currentNode:protected] => Lambert\TreeShape\Node Object
                (
                    [data:protected] => Array
                        (
                            [id] => 2
                            [name] => 节点2
                            [pid] => 0
                        )

                )

            [leftTree:protected] =>
            [rightTree:protected] =>
        )

)

Methods

  • 查找某个Tree

    $tree = $manager->findTree('name', '子节点1');
  • 获取当前树的左子树

    $leftTree = $tree->getLeftTree();
  • 获取当前树的右子树

    $rightTree = $tree->getRightTree();

    注意:这里如果是多个右子树,则为Array

  • 获取当前树的节点

    $node = $tree->getCurrentNode();
  • 获取节点属性
    继承了ArrayAccess,可以使用数组或属性的方式访问
    $node['name'] or $node->name;   // 节点1
    $node['id'] or $node->id;   // 1
    ...

如果客官觉得还行,别吝啬,给小弟一个大大赞吧,附上地址github

such