PHP code example of vhood / tree-converter

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

    

vhood / tree-converter example snippets


use Vhood\TreeType\Converter;
use Vhood\TreeType\Type\AdjacencyList;

$adjacencyList = [
    [
        'id' => 1,
        'name' => 'node1',
        'parent_id' => null,
    ],
    [
        'id' => 2,
        'name' => 'node2',
        'parent_id' => 1,
    ],
];

$adjacencyListConverter = new Converter(new AdjacencyList($adjacencyList));

print_r($adjacencyListConverter->toAssociativeArrayTree('children', 'id'));

// Array
// (
//     [0] => Array
//         (
//             [id] => 1
//             [name] => node1
//             [children] => Array
//                 (
//                     [0] => Array
//                         (
//                             [id] => 2
//                             [name] => node2
//                             [children] => Array
//                                 (
//                                 )

//                         )

//                 )

//         )

// )

use Vhood\TreeType\Converter;
use Vhood\TreeType\Type\MaterializedPath;

$materializedPath = [
    [
        'name' => 'node1',
        'path' => '/1/',
    ],
    [
        'name' => 'node2',
        'path' => '/1/2/',
    ],
];

$materializedPathConverter = new Converter(new MaterializedPath($materializedPath));

print_r($materializedPathConverter->toMaterializedPath('path', '/', 'level'));

// Array
// (
//     [0] => Array
//         (
//             [name] => node1
//             [path] => /1/
//             [level] => 1
//         )

//     [1] => Array
//         (
//             [name] => node2
//             [path] => /1/2/
//             [level] => 2
//         )

// )