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 ...
随机推荐
- am335x uart分析
/************************************************************ * am335x uart分析 * 本文记录am335x uart驱动的注册 ...
- Spring Boot集成Mybatis及通用Mapper
集成Mybatis可以通过 mybatis-spring-boot-starter 实现. <!-- https://mvnrepository.com/artifact/org.mybatis ...
- tarjan算法-解决有向图中求强连通分量的利器
小引 看到这个名词-tarjan,大家首先想到的肯定是又是一个以外国人名字命名的算法.说实话真的是很佩服那些算法大牛们,佩服得简直是五体投地啊.今天就遇到一道与求解有向图中强连通分量的问题,我的思路就 ...
- Winform appconfig修改后的更新问题
- jquery.attach附件上传jquery插件
html: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv=&qu ...
- 学习 TList 类的实现[1]
最近整理了一些函数列表, 算是一个宏观的安排; 等以后再碰到一些函数时就可以放置的更有次序一些. 我对函数与类的理解是: 函数是一个功能模块, 类是一个更强大的功能模块; Delphi 已经提供了很多 ...
- Office密码破解不求人!
你用Office吗?你会为你的Office文档加密吗?如果Office密码忘了求人吗?最后一个问题是不是让你很头大,求人办事不是要费钱就是要靠人情,不如自己拥有一款强大的密码破解工具,想要Office ...
- Chrome浏览器无法观看视频,一直提示“adobe flash player 已过期” ?
很多新用户在安装了Chrome浏览器或者更新过的的时候,经常提示“ adobe flash player 已过期”的问题,反复提示,导致无法观看视频.于是从网上也找了很多办法都没有解决.这里给大 ...
- Oracle Merge Into Insert/Update
出自:http://blog.csdn.net/yuzhic/article/details/1896878 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明 ...
- Python学习笔记(五)OOP
模块 使用模块import 模块名.有的仅仅导入了某个模块的一个类或者函数,使用from 模块名 import 函数或类名实现.为了避免模块名冲突.Python引入了按文件夹来组织模块的方法,称为包( ...