IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> PHP无限级分类实现 -> 正文阅读

[PHP知识库]PHP无限级分类实现

Array
(

[0] => Array
    (
        [id] => 2
        [pid] => 0
        [create_time] => 1626248460
        [update_time] => 0
        [source_name] => 三方
        [department_number] => 
        [hid] => 1
    )

[1] => Array
    (
        [id] => 3
        [pid] => 0
        [create_time] => 1626248631
        [update_time] => 0
        [source_name] => 竞价
        [department_number] => 
        [hid] => 1
    )

[2] => Array
    (
        [id] => 4
        [pid] => 0
        [create_time] => 1626248641
        [update_time] => 0
        [source_name] => 新媒体
        [department_number] => 
        [hid] => 1
    )

[3] => Array
    (
        [id] => 5
        [pid] => 0
        [create_time] => 1626249029
        [update_time] => 1626249489
        [source_name] => 电商1
        [department_number] => 
        [hid] => 1
    )

[4] => Array
    (
        [id] => 6
        [pid] => 5
        [create_time] => 1626249957
        [update_time] => 1626250184
        [source_name] => 电商23
        [department_number] => 
        [hid] => 1
    )

[5] => Array
    (
        [id] => 8
        [pid] => 4
        [create_time] => 1626250215
        [update_time] => 0
        [source_name] => 新媒体1
        [department_number] => 
        [hid] => 1
    )

[6] => Array
    (
        [id] => 9
        [pid] => 5
        [create_time] => 1626251341
        [update_time] => 0
        [source_name] => 电商33
        [department_number] => 
        [hid] => 1
    )

[7] => Array
    (
        [id] => 10
        [pid] => 5
        [create_time] => 1626251347
        [update_time] => 0
        [source_name] => 电商44
        [department_number] => 
        [hid] => 1
    )

[8] => Array
    (
        [id] => 11
        [pid] => 3
        [create_time] => 1626312001
        [update_time] => 0
        [source_name] => 竞价竞价竞
        [department_number] => 
        [hid] => 1
    )

[9] => Array
    (
        [id] => 13
        [pid] => 0
        [create_time] => 1626678565
        [update_time] => 0
        [source_name] => 渠道来源
        [department_number] => 
        [hid] => 1
    )

[10] => Array
    (
        [id] => 14
        [pid] => 13
        [create_time] => 1626678574
        [update_time] => 0
        [source_name] => 渠道来源子类
        [department_number] => 
        [hid] => 1
    )

)

非递归实现
//第一步,将分类id作为数组key,并创建children单元

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

//第二步,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。

foreach($tree as $key=>$item){ 
    if($item['pid'] != 0){
        $tree[$item['pid']]['children'][] = &$tree[$key];//注意:此处必须传引用否则结果不对
        if($tree[$key]['children'] == null){
            unset($tree[$key]['children']); //如果children为空,则删除该children元素(可选)
        }
    }
}

//第三步,删除无用的非根节点数据

foreach($tree as $key=>$category){
    if($category['pid'] != 0){
        unset($tree[$key]);
    }
}
echo "<pre>";
print_r($tree);
echo "<pre>";exit; 

运行结果如下
Array
(

[2] => Array
    (
        [id] => 2
        [pid] => 0
        [create_time] => 1626248460
        [update_time] => 0
        [source_name] => 三方
        [department_number] => 
        [hid] => 1
        [children] => Array
            (
            )
    )

[3] => Array
    (
        [id] => 3
        [pid] => 0
        [create_time] => 1626248631
        [update_time] => 0
        [source_name] => 竞价
        [department_number] => 
        [hid] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 11
                        [pid] => 3
                        [create_time] => 1626312001
                        [update_time] => 0
                        [source_name] => 竞价竞价竞
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

[4] => Array
    (
        [id] => 4
        [pid] => 0
        [create_time] => 1626248641
        [update_time] => 0
        [source_name] => 新媒体
        [department_number] => 
        [hid] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 8
                        [pid] => 4
                        [create_time] => 1626250215
                        [update_time] => 0
                        [source_name] => 新媒体1
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

[5] => Array
    (
        [id] => 5
        [pid] => 0
        [create_time] => 1626249029
        [update_time] => 1626249489
        [source_name] => 电商1
        [department_number] => 
        [hid] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 6
                        [pid] => 5
                        [create_time] => 1626249957
                        [update_time] => 1626250184
                        [source_name] => 电商23
                        [department_number] => 
                        [hid] => 1
                    )

                [1] => Array
                    (
                        [id] => 9
                        [pid] => 5
                        [create_time] => 1626251341
                        [update_time] => 0
                        [source_name] => 电商33
                        [department_number] => 
                        [hid] => 1
                    )

                [2] => Array
                    (
                        [id] => 10
                        [pid] => 5
                        [create_time] => 1626251347
                        [update_time] => 0
                        [source_name] => 电商44
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

[13] => Array
    (
        [id] => 13
        [pid] => 0
        [create_time] => 1626678565
        [update_time] => 0
        [source_name] => 渠道来源
        [department_number] => 
        [hid] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 14
                        [pid] => 13
                        [create_time] => 1626678574
                        [update_time] => 0
                        [source_name] => 渠道来源子类
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

)

引用算法

$items = array();
foreach($channelLists as $v){
    $items[$v['id']] = $v;
}
$tree = array();
foreach($items as $k => $item){
    if(isset($items[$item['pid']])){
        $items[$item['pid']]['son'][] = &$items[$k];
    }else{
        $tree[] = &$items[$k];
    }
}
 echo "<pre>";
 print_r($tree);
 echo "<pre>";exit; 

运行结果如下
Array
(

[0] => Array
    (
        [id] => 2
        [pid] => 0
        [create_time] => 1626248460
        [update_time] => 0
        [source_name] => 三方
        [department_number] => 
        [hid] => 1
    )

[1] => Array
    (
        [id] => 3
        [pid] => 0
        [create_time] => 1626248631
        [update_time] => 0
        [source_name] => 竞价
        [department_number] => 
        [hid] => 1
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 11
                        [pid] => 3
                        [create_time] => 1626312001
                        [update_time] => 0
                        [source_name] => 竞价竞价竞
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

[2] => Array
    (
        [id] => 4
        [pid] => 0
        [create_time] => 1626248641
        [update_time] => 0
        [source_name] => 新媒体
        [department_number] => 
        [hid] => 1
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 8
                        [pid] => 4
                        [create_time] => 1626250215
                        [update_time] => 0
                        [source_name] => 新媒体1
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

[3] => Array
    (
        [id] => 5
        [pid] => 0
        [create_time] => 1626249029
        [update_time] => 1626249489
        [source_name] => 电商1
        [department_number] => 
        [hid] => 1
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 6
                        [pid] => 5
                        [create_time] => 1626249957
                        [update_time] => 1626250184
                        [source_name] => 电商23
                        [department_number] => 
                        [hid] => 1
                    )

                [1] => Array
                    (
                        [id] => 9
                        [pid] => 5
                        [create_time] => 1626251341
                        [update_time] => 0
                        [source_name] => 电商33
                        [department_number] => 
                        [hid] => 1
                    )

                [2] => Array
                    (
                        [id] => 10
                        [pid] => 5
                        [create_time] => 1626251347
                        [update_time] => 0
                        [source_name] => 电商44
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

[4] => Array
    (
        [id] => 13
        [pid] => 0
        [create_time] => 1626678565
        [update_time] => 0
        [source_name] => 渠道来源
        [department_number] => 
        [hid] => 1
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 14
                        [pid] => 13
                        [create_time] => 1626678574
                        [update_time] => 0
                        [source_name] => 渠道来源子类
                        [department_number] => 
                        [hid] => 1
                    )
            )
    )

)

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-07-30 12:32:03  更:2021-07-30 12:33:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 2:55:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码