php - 实现无限级分类有几种方法呢??
大家讲道理
大家讲道理 2017-04-11 09:01:21
[PHP讨论组]

除了使用递归的方式实现之外还有什么方式可以实现无限级呢??

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(4)
怪我咯
$categories = [
  ['id'=>1,'cat_id'=>1,'cat_name'=>'a','pid'=>0],
  ['id'=>2,'cat_id'=>2,'cat_name'=>'b','pid'=>1],
  ['id'=>3,'cat_id'=>3,'cat_name'=>'c','pid'=>1],
  ['id'=>4,'cat_id'=>4,'cat_name'=>'d','pid'=>2],
  ['id'=>5,'cat_id'=>5,'cat_name'=>'e','pid'=>3],
];
    
    
$tree = [];

foreach($categories as $v){
    $tree[$v['id']] = $v;
    $tree[$v['id']]['children'] = array();
}

foreach ($tree as $k=>$v) {
    if ($v['pid'] > 0) {
        $tree[$v['pid']]['children'][] = &$tree[$k];
    }
}    
print_r($tree);
PHP中文网

可以 自连接递归

天蓬老师

使用递归

function _data_to_tree(&$items, $topid = 0, $with_id = TRUE)
{
    $result = [];
    foreach($items as $v)
        if ($topid == $v['parent'])  {
            $r = $v + ['children' => _data_to_tree($items, $v['id'], $with_id)];
            if ($with_id)
                $result[$v['id']] = $r;
            else
                $result[] = $r;
        }
            
    return $result;
}

使用PHP的指针特性

function _data_to_tree($items, $topid = 0, $with_id = TRUE)
{
    if ($with_id)
        foreach ($items as $item)
            $items[ $item['parent'] ]['children'][ $item['id'] ] = &$items[ $item['id'] ];
    else
        foreach ($items as $item)
                $items[ $item['parent'] ]['children'][] = &$items[ $item['id'] ];

         return isset($items[ $topid ]['children']) ? $items[ $topid ][ 'children' ] : [];
}
//注意本算法 不会输出 0 的根节点
//并且数据必须有KEY,并且需要与id相等,也就是如下格式:
// 1 => ['id' => 1]
// 2 => ['id' => 2] 

使用

传入你的上述数组,比如最顶层的ID为0

$data = [
  4 => ['id' => 4, 'parent' => 1 , 'text' => 'Parent1'], 
  1 => ['id' => 1, 'parent' => 0 , 'text' => 'Root'],
  2 => ['id' => 2, 'parent' => 1 , 'text' => 'Parent2'], 
  3 => ['id' => 3, 'parent' => 2 , 'text' => 'Sub1'], 
];
print_r ( _data_to_tree($data, 0) );

结果

Array
(
    [1] => Array
        (
            [id] => 1
            [parent] => 0
            [text] => Root
            [children] => Array
                (
                    [4] => Array
                        (
                            [id] => 4
                            [parent] => 1
                            [text] => Parent1
                            [children] => Array
                                (
                                )
                        )
                    [2] => Array
                        (
                            [id] => 2
                            [parent] => 1
                            [text] => Parent2
                            [children] => Array
                                (
                                    [3] => Array
                                        (
                                            [id] => 3
                                            [parent] => 2
                                            [text] => Sub1
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
)
天蓬老师

可以迭代循环

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号