PHP code example of odannyc / treeize

1. Go to this page and download the library: Download odannyc/treeize 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/ */

    

odannyc / treeize example snippets


Treeize::create($tree)->parse()->get();

$tree = [
  [
    "id" => 1,
    "parent_id" => 0
  ],
  [
    "id" => 2,
    "parent_id" => 1
  ],
  [
    "id" => 3,
    "parent_id" => 1
  ],
  [
    "id" => 4,
    "parent_id" => 2
  ],
  [
    "id" => 5,
    "parent_id" => 2
  ],[
    "id" => 6,
    "parent_id" => 0
  ],
  [
    "id" => 7,
    "parent_id" => 6
  ],
  [
    "id" => 8,
    "parent_id" => 6
  ]
]

Treeize::create($tree)->parse()->get();

[
  [
    "id" => 1,
    "parent_id" => 0,
    "children" => [
      [
        "id" => 2,
        "parent_id" => 1,
        "children" => [
          [
            "id" => 4,
            "parent_id" => 2
          ],
          [
            "id" => 5,
            "parent_id" => 2
          ]
        ]
      ],
      [
        "id" => 3,
        "parent_id" => 1
      ],
    ]
  ],
  [
    "id" => 6,
    "parent_id" => 0,
    "children" => [
      [
        "id" => 7,
        "parent_id" => 6
      ],
      [
        "id" => 8,
        "parent_id" => 6
      ]
    ]
  ]
]

Treeize::create($tree)->childrenKey('nodes')->parse()->get();

Treeize::create($tree)
  ->parentKey('pid')
  ->indexKey('id')
  ->parentId(0)
  ->childrenKey('nodes')
  ->parse(function($item) {//do stuff to item})
  ->get();