https://github.com/codrasil/tree
A PHP Library to generate hierarchical tree list
https://github.com/codrasil/tree
hierarchical menus nested-structures sidebar tree treeview
Last synced: 2 months ago
JSON representation
A PHP Library to generate hierarchical tree list
- Host: GitHub
- URL: https://github.com/codrasil/tree
- Owner: codrasil
- License: mit
- Created: 2019-02-09T00:16:41.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-21T08:31:22.000Z (almost 7 years ago)
- Last Synced: 2025-08-22T19:46:22.014Z (7 months ago)
- Topics: hierarchical, menus, nested-structures, sidebar, tree, treeview
- Language: PHP
- Homepage: https://packagist.org/packages/codrasil/tree
- Size: 1.14 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Codrasil / Tree
[](https://packagist.org/packages/codrasil/tree)
[](https://packagist.org/packages/codrasil/tree)
[](https://packagist.org/packages/codrasil/tree)
[](https://packagist.org/packages/codrasil/tree)
PHP Library to generate a hierarchical tree list from array. Ideal for creating menus, sidebars, and any nested resources.
#### Requirements
* **php**: `>=7.1`
#### Installation
```bash
$ composer require codrasil/tree
```
```json
{
"require": {
"codrasil/tree": "^1.*"
}
}
```
#### Usage
```php
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 ( ... )
*/
```
You may also run the demo inside the tests folder via cloning this repository, then starting a php server on the `tests/` folder.
```bash
cd /path/to/codrasil/tree
php -S localhost:8080 -t tests/
```

#### Documentation
coming soon
#### License
The Codrasil/Tree PHP library is open-source software licensed under the [MIT license](./LICENSE).