PHP code example of codrasil / tree

1. Go to this page and download the library: Download codrasil/tree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codrasil / tree example snippets


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 ( ... )
*/