/**
* 把返回的数据集转换成Tree
* @param array $list 要转换的数据集
* @param string $pk 自增字段(栏目id)
* @param string $pid parent标记字段
* @return array
* @author dqs <1696232133@qq.com>
*/
function make_tree($list,$pk='id',$pid='pid',$child='_child',$root=0){
$tree=array();
$packData=array();
foreach ($list as $data) {
$packData[$data[$pk]] = $data;
}
foreach ($packData as $key =>$val){
if($val[$pid]==$root){//代表跟节点
$tree[]=& $packData[$key];
}else{
//找到其父类
$packData[$val[$pid]][$child][]=& $packData[$key];
}
}
return $tree;
}
Array
(
[0] => Array
(
[id] => 6
[pid] => 0
[title] => 测试1
[url] => test1
[sort] => 0
[create_time] => 1464076199
[update_time] => 1464076199
[status] => 1
[target] => 0
)
[1] => Array
(
[id] => 7
[pid] => 0
[title] => 测试2
[url] => test2
[sort] => 0
[create_time] => 1464076218
[update_time] => 1464076218
[status] => 1
[target] => 0
)
[2] => Array
(
[id] => 1
[pid] => 0
[title] => 首页
[url] => Index/index
[sort] => 1
[create_time] => 1379475111
[update_time] => 1379923177
[status] => 1
[target] => 0
)
[3] => Array
(
[id] => 2
[pid] => 0
[title] => 博客
[url] => Article/index?category=blog
[sort] => 2
[create_time] => 1379475131
[update_time] => 1379483713
[status] => 1
[target] => 0
[_child] => Array
(
[0] => Array
(
[id] => 4
[pid] => 2
[title] => 个人博客
[url] => ownblog
[sort] => 0
[create_time] => 1464075558
[update_time] => 1464075558
[status] => 1
[target] => 0
[_child] => Array
(
[0] => Array
(
[id] => 8
[pid] => 4
[title] => 新浪博客
[url] => sinaBlog
[sort] => 0
[create_time] => 1464077276
[update_time] => 1464077276
[status] => 1
[target] => 0
)
)
)
[1] => Array
(
[id] => 5
[pid] => 2
[title] => 他人博客
[url] => otherBlog
[sort] => 0
[create_time] => 1464075582
[update_time] => 1464075582
[status] => 1
[target] => 0
)
)
)
[4] => Array
(
[id] => 3
[pid] => 0
[title] => 官网
[url] => http://www.onethink.cn
[sort] => 3
[create_time] => 1379475154
[update_time] => 1387163458
[status] => 1
[target] => 0
)
)
/**
* 把返回的数据集转换成Tree
* @param array $list 要转换的数据集
* @param string $pid parent标记字段
* @param string $level level标记字段
* @return array
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
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;
}
function make_tree1($list,$pk='id',$pid='pid',$child='_child',$root=0){
$tree=array();
foreach($list as $key=> $val){
if($val[$pid]==$root){
//获取当前$pid所有子类
unset($list[$key]);
if(! empty($list)){
$child=make_tree1($list,$pk,$pid,$child,$val[$pk]);
if(!empty($child)){
$val['_child']=$child;
}
}
$tree[]=$val;
}
}
return $tree;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有