PHP code example of ajiho / hierarchy-builder

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

    

ajiho / hierarchy-builder example snippets


$data = [
    ['id' => 8, 'name' => '白云区', 'pid' => 6],
    ['id' => 2, 'name' => '广东省', 'pid' => 1],
    ['id' => 1, 'name' => '中国', 'pid' => 0],
    ['id' => 9, 'name' => '美国', 'pid' => 0],
    ['id' => 6, 'name' => '广州市', 'pid' => 2],
    ['id' => 7, 'name' => '海珠区', 'pid' => 6],
    ['id' => 4, 'name' => '龙岗区', 'pid' => 3],
    ['id' => 10, 'name' => '加利福尼亚州', 'pid' => 9],
    ['id' => 5, 'name' => '南山区', 'pid' => 3],
    ['id' => 11, 'name' => '洛杉矶市', 'pid' => 10],
    ['id' => 3, 'name' => '深圳市', 'pid' => 2],
];


$builder = (new HierarchyBuilder($data));
//或者
$builder = HierarchyBuilder::create($data);

//如果您的关系字段默认不是id和pid
$builder->setIdField('id');
$builder->setPidField('pid');
//也可以指定生成的层级关系字段和表示子集的字段
$builder->setLevelField('level');
$builder->setSonField('children');


$results = $builder->getCateList($pid = 0, $level = 0);
$results = $builder->getTreeList($pid = 0);
$results = $builder->getParents($id, $pid = 0, $

$results = HierarchyBuilder::create($data)->getCateList();

//结果如下
$results = [
    ['id' => 1, 'name' => '中国', 'pid' => 0, 'level' => 0],
    ['id' => 2, 'name' => '广东省', 'pid' => 1, 'level' => 1],
    ['id' => 6, 'name' => '广州市', 'pid' => 2, 'level' => 2],
    ['id' => 8, 'name' => '白云区', 'pid' => 6, 'level' => 3],
    ['id' => 7, 'name' => '海珠区', 'pid' => 6, 'level' => 3],
    ['id' => 3, 'name' => '深圳市', 'pid' => 2, 'level' => 2],
    ['id' => 4, 'name' => '龙岗区', 'pid' => 3, 'level' => 3],
    ['id' => 5, 'name' => '南山区', 'pid' => 3, 'level' => 3],
    ['id' => 9, 'name' => '美国', 'pid' => 0, 'level' => 0],
    ['id' => 10, 'name' => '加利福尼亚州', 'pid' => 9, 'level' => 1],
    ['id' => 11, 'name' => '洛杉矶市', 'pid' => 10, 'level' => 2],
];



// 获取id为2下面所有的子集分类列表
$results2 = HierarchyBuilder::create($data)->getCateList(2);
//结果如下
$results2 = [
    ['id' => 6, 'name' => '广州市', 'pid' => 2, 'level' => 0],
    ['id' => 8, 'name' => '白云区', 'pid' => 6, 'level' => 1],
    ['id' => 7, 'name' => '海珠区', 'pid' => 6, 'level' => 2],
    ['id' => 3, 'name' => '深圳市', 'pid' => 2, 'level' => 0],
    ['id' => 4, 'name' => '龙岗区', 'pid' => 3, 'level' => 1],
    ['id' => 5, 'name' => '南山区', 'pid' => 3, 'level' => 2],
];



// 设定level的层级从1开始自增
$results3 = HierarchyBuilder::create($data)->getCateList(6,1);

//结果如下
$results3 = [
    ['id' => 8, 'name' => '白云区', 'pid' => 6, 'level' => 1],
    ['id' => 7, 'name' => '海珠区', 'pid' => 6, 'level' => 1],
];

$results = HierarchyBuilder::create($data)->getTreeList();
//结果如下
$results = [
    ['id' => 1,
        'name' => '中国',
        'pid' => 0,
        'children' => [
            [
                'id' => 2,
                'name' => '广东省',
                'pid' => 1,
                'children' => [
                    [
                        'id' => 6,
                        'name' => '广州市',
                        'pid' => 2,
                        'children' => [
                            [
                                'id' => 8,
                                'name' => '白云区',
                                'pid' => 6,
                                'children' => []
                            ],
                            [
                                'id' => 7,
                                'name' => '海珠区',
                                'pid' => 6,
                                'children' => []
                            ],
                        ]
                    ],
                    [
                        'id' => 3,
                        'name' => '深圳市',
                        'pid' => 2,
                        'children' => [
                            [
                                'id' => 4,
                                'name' => '龙岗区',
                                'pid' => 3,
                                'children' => []
                            ],
                            [
                                'id' => 5,
                                'name' => '南山区',
                                'pid' => 3,
                                'children' => []
                            ],
                        ]
                    ],
                ]
            ],
        ]
    ],
    ['id' => 9,
        'name' => '美国',
        'pid' => 0,
        'children' => [
            [
                'id' => 10,
                'name' => '加利福尼亚州',
                'pid' => 9,
                'children' => [
                    [
                        'id' => 11,
                        'name' => '洛杉矶市',
                        'pid' => 10,
                        'children' => []
                    ],
                ]
            ],
        ]
    ],
];


//更改子节点的字段为son
$results2 = HierarchyBuilder::create($data)->setSonField('son')->getTreeList();
//结果如下
$results2 = [
    ['id' => 1,
        'name' => '中国',
        'pid' => 0,
        'son' => [
            [
                'id' => 2,
                'name' => '广东省',
                'pid' => 1,
                'son' => [
                    [
                        'id' => 6,
                        'name' => '广州市',
                        'pid' => 2,
                        'son' => [
                            [
                                'id' => 8,
                                'name' => '白云区',
                                'pid' => 6,
                                'son' => []
                            ],
                            [
                                'id' => 7,
                                'name' => '海珠区',
                                'pid' => 6,
                                'son' => []
                            ],
                        ]
                    ],
                    [
                        'id' => 3,
                        'name' => '深圳市',
                        'pid' => 2,
                        'son' => [
                            [
                                'id' => 4,
                                'name' => '龙岗区',
                                'pid' => 3,
                                'son' => []
                            ],
                            [
                                'id' => 5,
                                'name' => '南山区',
                                'pid' => 3,
                                'son' => []
                            ],
                        ]
                    ],
                ]
            ],
        ]
    ],
    ['id' => 9,
        'name' => '美国',
        'pid' => 0,
        'son' => [
            [
                'id' => 10,
                'name' => '加利福尼亚州',
                'pid' => 9,
                'son' => [
                    [
                        'id' => 11,
                        'name' => '洛杉矶市',
                        'pid' => 10,
                        'son' => []
                    ],
                ]
            ],
        ]
    ]
];


$results = HierarchyBuilder::create($data)->getParents(8);
//结果如下
$results = [
    ['id' => 8, 'name' => '白云区', 'pid' => 6]
    ['id' => 6, 'name' => '广州市', 'pid' => 2],
    ['id' => 2, 'name' => '广东省', 'pid' => 1],
    ['id' => 1, 'name' => '中国', 'pid' => 0],
];

//搜索到pid为1停止
$results2 = HierarchyBuilder::create($data)->getParents(8,1);
//结果如下
$results2 = [
    ['id' => 8, 'name' => '白云区', 'pid' => 6]
    ['id' => 6, 'name' => '广州市', 'pid' => 2],
    ['id' => 2, 'name' => '广东省', 'pid' => 1]
];

//搜索结果不包含当前传入的id节点本身
$results3 = HierarchyBuilder::create($data)->getParents(8,1,false);
//结果如下
$results3 = [
    ['id' => 6, 'name' => '广州市', 'pid' => 2],
    ['id' => 2, 'name' => '广东省', 'pid' => 1]
];