PHP code example of collinai / auxiliary-open

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

    

collinai / auxiliary-open example snippets


use Collinai\AuxiliaryOpen\Tree;

// 示例数据数组
$data = [
    ['id' => 1, 'parent_id' => 0, 'name' => '节点 1'],
    ['id' => 2, 'parent_id' => 1, 'name' => '节点 1.1'],
    ['id' => 3, 'parent_id' => 1, 'name' => '节点 1.2'],
    ['id' => 4, 'parent_id' => 0, 'name' => '节点 2'],
    ['id' => 5, 'parent_id' => 4, 'name' => '节点 2.1']
];

// 实例化Tree类
$tree = new Tree();

// 初始化数据
$tree->init($data, 'parent_id', 'id');

// 构建树形结构
$treeStructure = $tree->buildTree();

// 输出结果查看
print_r($treeStructure);