OneThink生成分类树方法(list_to_tree)使用!
具体方法:
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)使用!的更多相关文章
- php 两种获取分类树的方法
php 两种获取分类树的方法 1. /** * 获取分类树 * @param array $array 数据源 * @param int $pid 父级ID * @param int $level 分 ...
- C#无限极分类树-创建-排序-读取 用Asp.Net Core+EF实现之方法二:加入缓存机制
在上一篇文章中我用递归方法实现了管理菜单,在上一节我也提到要考虑用缓存,也算是学习一下.Net Core的缓存机制. 关于.Net Core的缓存,官方有三种实现: 1.In Memory Cachi ...
- C#无限极分类树-创建-排序-读取 用Asp.Net Core+EF实现
今天做一个管理后台菜单,想着要用无限极分类,记得园子里还是什么地方见过这种写法,可今天找了半天也没找到,没办法静下心来自己写了: 首先创建节点类(我给它取名:AdminUserTree): /// & ...
- 决策树算法原理(CART分类树)
决策树算法原理(ID3,C4.5) CART回归树 决策树的剪枝 在决策树算法原理(ID3,C4.5)中,提到C4.5的不足,比如模型是用较为复杂的熵来度量,使用了相对较为复杂的多叉树,只能处理分类不 ...
- sklearn 学习之分类树
概要 基于 sklearn 包自带的 iris 数据集,了解一下分类树的各种参数设置以及代表的意义. iris 数据集介绍 iris 数据集包含 150 个样本,对应数据集的每行数据,每行数据包含 ...
- 机器学习实战---决策树CART简介及分类树实现
https://blog.csdn.net/weixin_43383558/article/details/84303339?utm_medium=distribute.pc_relevant_t0. ...
- LinqToDB 源码分析——生成表达式树
当我们知道了Linq查询要用到的数据库信息之后.接下就是生成对应的表达式树.在前面的章节里面笔者就已经介绍过.生成表达式树是事实离不开IQueryable<T>接口.而处理表达式树离不开I ...
- PHP+Mysql无限分类的方法汇总
无限分类是个老话题了,来看看PHP结合Mysql如何实现.第一种方法这种方法是很常见.很传统的一种,先看表结构表:categoryid int 主键,自增name varchar 分类名称pid in ...
- 泛型方法动态生成表达式树 Expression
public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...
随机推荐
- SQL SERVER 2005快捷键
一.SQL SERVER 2005快捷键 快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 ...
- CSS控制显示图片的一部分
使用情形:防止反复请求图片资源,我们经常采用一张图片多种效果或内容显示. 假设我有纸张竖直方向的一张图片,竖直y轴方向分别是字母:A,B,C.... 现在分别要显示A.B.C 等字母,我们的CSS可以 ...
- (转)从海康7816的ps流里获取数据h264数据
海康7816使用ps流来封装h.264数据,这里使用的解码器无法识别ps流,因此需要将h264数据从ps流里提取出来 对于ps流的规定可以参考13818-1文档 这里从7816里获取到一些数据取样 0 ...
- (转)分析kernel的initcall函数
分析kernel的initcall函数 来源: ChinaUnix博客 日期: 2008.07.19 21:24 (共有条评论) 我要评论 分析kernel的initcall函数Autho ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- storm深入研究
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:He Ransom链接:http://www.zhihu.com/question/23441639/answer/28075 ...
- jQuery 检查某个元素在页面上是否存在实例代码
用jQuery检查某个元素在网页上是否存在时,应该根据获取元素的长度来判断,代码如下: if($("#tt").length > 0) { //元素存在时执行的代码 } ...
- IE6图片元素img下出现多余空白问题
在进行页面的 DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对于该问题的解决方法 也是“见机行事”,根据原因的不同要用不同 ...
- swift - UISegmentedControl 和 UIWebView 的用法
这两个用法比较简单: 具体代码如下: 一.UISegmentedControl 1.UISegmentedControl的声明 var segment = UISegmentedControl() 2 ...
- ios 6以后,UILabel全属性
一.初始化 1 UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 120, 44)]; 2 3 [s ...