具体方法:

Application / Common / Common / function.php 下的 224行:

function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}

具体使用:例如这样的一个数组:

array(9) {
[0]=>
array(6) {
["id"]=>
string(2) "39"
["title"]=>
string(12) "医院概况"
["pid"]=>
string(1) "0"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
}
[1]=>
array(6) {
["id"]=>
string(2) "42"
["title"]=>
string(12) "新闻中心"
["pid"]=>
string(1) "0"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
}
[2]=>
array(6) {
["id"]=>
string(2) "46"
["title"]=>
string(12) "问答分类"
["pid"]=>
string(1) "0"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "5"
}
[3]=>
array(6) {
["id"]=>
string(2) "40"
["title"]=>
string(12) "医院简介"
["pid"]=>
string(2) "39"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
}
[4]=>
array(6) {
["id"]=>
string(2) "41"
["title"]=>
string(12) "领导班子"
["pid"]=>
string(2) "39"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
}
[5]=>
array(6) {
["id"]=>
string(2) "43"
["title"]=>
string(12) "医院新闻"
["pid"]=>
string(2) "42"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
}
[6]=>
array(6) {
["id"]=>
string(2) "44"
["title"]=>
string(12) "行业新闻"
["pid"]=>
string(2) "42"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
}
[7]=>
array(6) {
["id"]=>
string(2) "45"
["title"]=>
string(12) "文化信息"
["pid"]=>
string(2) "42"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
}
[8]=>
array(6) {
["id"]=>
string(2) "47"
["title"]=>
string(12) "其他新闻"
["pid"]=>
string(2) "43"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
}
}

进行分类:

$cate =   list_to_tree($cate);    //生成分类树
pd($cate);

效果:

array(3) {
[0]=>
array(7) {
["id"]=>
string(2) "39"
["title"]=>
string(12) "医院概况"
["pid"]=>
string(1) "0"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
["_child"]=>
array(2) {
[0]=>
array(6) {
["id"]=>
string(2) "40"
["title"]=>
string(12) "医院简介"
["pid"]=>
string(2) "39"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
}
[1]=>
array(6) {
["id"]=>
string(2) "41"
["title"]=>
string(12) "领导班子"
["pid"]=>
string(2) "39"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
}
}
}
[1]=>
array(7) {
["id"]=>
string(2) "42"
["title"]=>
string(12) "新闻中心"
["pid"]=>
string(1) "0"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
["_child"]=>
array(3) {
[0]=>
array(7) {
["id"]=>
string(2) "43"
["title"]=>
string(12) "医院新闻"
["pid"]=>
string(2) "42"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
["_child"]=>
array(1) {
[0]=>
array(6) {
["id"]=>
string(2) "47"
["title"]=>
string(12) "其他新闻"
["pid"]=>
string(2) "43"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
}
}
}
[1]=>
array(6) {
["id"]=>
string(2) "44"
["title"]=>
string(12) "行业新闻"
["pid"]=>
string(2) "42"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
string(1) "4"
["model"]=>
string(1) "2"
}
[2]=>
array(6) {
["id"]=>
string(2) "45"
["title"]=>
string(12) "文化信息"
["pid"]=>
string(2) "42"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "2"
}
}
}
[2]=>
array(6) {
["id"]=>
string(2) "46"
["title"]=>
string(12) "问答分类"
["pid"]=>
string(1) "0"
["allow_publish"]=>
string(1) "1"
["cattype"]=>
NULL
["model"]=>
string(1) "5"
}
}

与其相反的还有 tree_to_list : Application / Common / Common / function.php 下的 258行:

function tree_to_list($tree, $child = '_child', $order='id', &$list = array()){
if(is_array($tree)) {
foreach ($tree as $key => $value) {
$reffer = $value;
if(isset($reffer[$child])){
unset($reffer[$child]);
tree_to_list($value[$child], $child, $order, $list);
}
$list[] = $reffer;
}
$list = list_sort_by($list, $order, $sortby='asc');
}
return $list;
}

OneThink生成分类树方法(list_to_tree)使用!的更多相关文章

  1. php 两种获取分类树的方法

    php 两种获取分类树的方法 1. /** * 获取分类树 * @param array $array 数据源 * @param int $pid 父级ID * @param int $level 分 ...

  2. C#无限极分类树-创建-排序-读取 用Asp.Net Core+EF实现之方法二:加入缓存机制

    在上一篇文章中我用递归方法实现了管理菜单,在上一节我也提到要考虑用缓存,也算是学习一下.Net Core的缓存机制. 关于.Net Core的缓存,官方有三种实现: 1.In Memory Cachi ...

  3. C#无限极分类树-创建-排序-读取 用Asp.Net Core+EF实现

    今天做一个管理后台菜单,想着要用无限极分类,记得园子里还是什么地方见过这种写法,可今天找了半天也没找到,没办法静下心来自己写了: 首先创建节点类(我给它取名:AdminUserTree): /// & ...

  4. 决策树算法原理(CART分类树)

    决策树算法原理(ID3,C4.5) CART回归树 决策树的剪枝 在决策树算法原理(ID3,C4.5)中,提到C4.5的不足,比如模型是用较为复杂的熵来度量,使用了相对较为复杂的多叉树,只能处理分类不 ...

  5. sklearn 学习之分类树

    概要 基于 sklearn 包自带的 iris 数据集,了解一下分类树的各种参数设置以及代表的意义.   iris 数据集介绍 iris 数据集包含 150 个样本,对应数据集的每行数据,每行数据包含 ...

  6. 机器学习实战---决策树CART简介及分类树实现

    https://blog.csdn.net/weixin_43383558/article/details/84303339?utm_medium=distribute.pc_relevant_t0. ...

  7. LinqToDB 源码分析——生成表达式树

    当我们知道了Linq查询要用到的数据库信息之后.接下就是生成对应的表达式树.在前面的章节里面笔者就已经介绍过.生成表达式树是事实离不开IQueryable<T>接口.而处理表达式树离不开I ...

  8. PHP+Mysql无限分类的方法汇总

    无限分类是个老话题了,来看看PHP结合Mysql如何实现.第一种方法这种方法是很常见.很传统的一种,先看表结构表:categoryid int 主键,自增name varchar 分类名称pid in ...

  9. 泛型方法动态生成表达式树 Expression

    public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...

随机推荐

  1. 另外一款超棒的响应式布局jQuery插件 – Freetile.js

    在线演示 我们曾经介绍过俩款知名的响应式布局插:isotope和masonary,今天我们这里再介绍一款相当不错的响应式布局插件 – Freetile.js,使用它同样可以生成超酷的动态布局效果.相信 ...

  2. 使用ffmpeg的av_read_frame,如何控制连接超时

    最近使用ffmpeg来做一个rtsp的客户端,这过程也遇到不少问题,不过相应都比较好,一路走下来.不过到项目结尾时,且遇到一个比较纠结的问题.那就是客户端在使用的过程中,把rtsp服务器的网断了.这时 ...

  3. php Laravel 框架之建立后台目录

    今天研究了在Laravel框架中的控制器中加入后台的目录.发现了一些小的规律,拿来和大家分享一下吧. 通常情况下,我们是直接在controllers目录中加入我们的控制器,然后再routes.php ...

  4. CentOS运维常用管理操作命令

    自己整理的整理Linux常用运维和linux常用管理操作命令,当然不是非常详细和丰富,但是也基本上够用了吧.欢迎留言补充更多的Linux常用运维和linux常用管理操作命令.不断完善中.... 备份m ...

  5. Hbase1.1.0.1配置集群

    参考链接 http://wuyudong.com/archives/119?utm_source=tuicool 参考链接 http://www.cnblogs.com/archimedes/p/45 ...

  6. linux下如何关闭防火墙、查看当前的状态、开放端口

    从配置菜单关闭防火墙是不起作用的,索性在安装的时候就不要装防火墙查看防火墙状态:/etc/init.d/iptables status暂时关闭防火墙:/etc/init.d/iptables stop ...

  7. Entity FrameWork实现增、删、改、分页的数据层代码实现

    public bool AddEntity(Customer entity)//添加实体   {       MyHotelModelContainer hotelDB = new MyHotelMo ...

  8. Java精选笔记_JSP技术

    JSP技术 JSP概述 什么是JSP 在JSP全名是Java Server Page,它是建立在Servlet规范之上的动态网页开发技术. 在JSP文件中,HTML代码与Java代码共同存在,其中,H ...

  9. Python 处理命令行参数

    optparse模块用于从命令行直接读取参数,用法基本与 argparse模块 一致,如下: #!/usr/bin/env python #-*- coding:utf-8 -*- from optp ...

  10. Android储存

    Android储存一共5种方法 一: 手机内置,外部储存 1.获取本地存储 (Android的读写文件及权限设置) getFilesDir()   data/data/包名/File getCache ...