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 ...
随机推荐
- C语言 static静态变量
静态变量类型说明符是static. 静态变量属于静态存储方式,其存储空间为内存中的静态数据区(在 静态存储区内分配存储单元),该区域中的数据在整个程序的运行期间一直占用这些存储空间(在程序整个运行期间 ...
- php header函数下载文件实现代码
在php中header函数的使用很大,header不但可以向客户端发送原始的 HTTP 报头信息,同时还可以直接实现文件下载操作 header函数最常用的不是用于下载而是用于发送http类的 跳转 它 ...
- perl 字符串比较操作符
perl 中数字和字符串的比较操作符是不一样的 : 其中 == 用于比较数字是否相等:eq 用于比较字符串是否相等: 今天找程序里的bug,结果就是这个操作符用错,哎,赶紧记一下!
- openal 基础知识4
二函数 1. buffer函数 void alGenBuffers(ALsizei n /* buffer数*/, ALuint * buffers /* buffer ID数组*/); void a ...
- 中文路径-接口路径url不能传输中文解决方案
服务端:
- js判断用户关闭页面或浏览器
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- shell脚本中,将所有的参数值否赋给一个变量或者说将所有的参数合成一个字符串,获取所有参数
需求描述: 在写脚本的过程中,遇到这样的一个需求,将脚本执行过程中,传递给 脚本的所有的参数,都赋值给一个变量然后在对这个变量进行处理. 测试过程: 通过以下的脚本将所有传递给脚本的变量都赋值一个变量 ...
- NHibernate实例
1. 下载相关资源: 下载NHibernate.下载地址: http://nhforge.org/Default.aspx 下载微软Northwind示例数据库,下载地址:http://www.mic ...
- Windows10下安装python(配置环境变量)
从官网下载Windows下的python版本,一路按照默认进行安装. 安装之后配置环境变量的步骤如下: 1,点“我的电脑”,右键选“属性”. 2,选择“高级系统设置”--->选“环境变量”--- ...
- file.wirtelines()方法【python】
转自:http://www.jb51.net/article/66643.htm