首页 技术 正文
技术 2022年11月23日
0 收藏 843 点赞 3,522 浏览 3867 个字

具体方法:

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;
}

微信扫一扫

支付宝扫一扫

本文网址:https://www.zhankr.net/141848.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:875 阅读:5,067
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:806 阅读:3,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:565 阅读:4,312
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:730 阅读:4,307
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:4,904
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,097
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:8:00-16:00

客服电话

400-888-8888

客服邮箱

ceotheme@ceo.com

扫描二维码

关注微信公众号

扫描二维码

手机访问本站