EasyTree 一個生成樹型資料結構最簡單的方式

8090Lambert發表於2018-01-10

前言

最近在做公司的專案開發,類似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

本作品採用《CC 協議》,轉載必須註明作者和本文連結
8090lambert

相關文章