codrasil/tree

PHP库,从数组生成层次树列表。

v1.0.2 2019-06-06 06:06 UTC

This package is auto-updated.

Last update: 2024-09-21 21:14:42 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

PHP库,从数组生成层次树列表。非常适合创建菜单、侧边栏以及任何嵌套资源。


要求

  • php: >=7.1

安装

$ composer require codrasil/tree
{
    "require": {
        "codrasil/tree": "^1.*"
    }
}

使用

use Codrasil\Tree\Tree;

$menus = [
  ['id' => 'menu:user', 'text' => 'Users', 'order' => 2, 'children' => [ ... ]],
  ['id' => 'menu:blog', 'text' => 'Blogs', 'order' => 1],
  ['id' => 'submenu:blog:category', 'parent' => 'menu:blog', 'text' => 'Blog Categories', 'order' => 1],
];

$options = [
  'key' => 'id',
  'parent' => 'parent',
  'children' => 'children',
];

$tree = new Tree($menus, $options);

$tree->build();

print_r($tree->get());
/*
output:

Array
(
    [menu:blog] => Codrasil\Tree\Branch Object
        (
            [key:protected] => menu:blog
            [parent:protected] => root
            [node:protected] => Array
                (
                    [name] => menu:blog
                    [parent] => root
                    [order] => 1
                    [children] => Array
                        (
                            [submenu:blog:category] => Codrasil\Tree\Branch Object
                                (
                                    ...
                                )
                        )
                    [left] => 2
                    [right] => 3
                )
        )
    [menu:user] => Codrasil\Tree\Branch Object ( ... )
*/

您也可以通过克隆此存储库并在 tests/ 文件夹上启动PHP服务器,在测试文件夹内运行演示。

cd /path/to/codrasil/tree
php -S localhost:8080 -t tests/

Screenshot of demo server


文档

即将推出


许可

Codrasil/Tree PHP库是开源软件,受MIT许可许可。