PHP code example of ucscode / tree-node

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

    

ucscode / tree-node example snippets


use Ucscode\TreeNode\TreeNode;

$ceo = new TreeNode('Ucscode');

// Create Children with (or without) name & attribute

$manager = new TreeNode('Elizabeth');

$staff = new TreeNode('Serena Paul', ['active' => true]);

$staff2 = new TreeNode(null, ['name' => 'Unknown'])

// Position each node under a dedicated parent

$ceo->addChild('manager', $manager);

$manager->addChild('staff', $staff);

$manager->addChild('staff2', $staff2);

$manager = $ceo->getChild('manager');

$manager->removeChild('staff');

$manager->getChildren(); // Array of TreeNodes

$staff2 = $treeNode->getParent(); // Manager

$ceo = $staff2
        ->getParent() // Manager
            ->getParent(); // Ceo

$staff2->getAttribute('name'); // "Unknown"

$ceo->setAttribute('location', 'Log Angeles');

$staff->removeAttribute('active');