/**
* 把返回的数据集转换成Tree
* @access public
* @param array $list 要转换的数据集
* @param string $pid parent标记字段
* @param string $level level标记字段
* @return array
*/
function list_to_tree2($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];//取出每个栏目id作为键名,
}
foreach ($list as $key => $data) {//开始循环,key从0开始一个一个往下走0123.。。
// 判断是否存在parent
$parentId = $data[$pid];//echo $parentId.'|';//0|112|116|117|112输出每个数组里的pid
if ($root == $parentId) {//key从0开始循环,如果是一级栏目pid==0
$tree[] =& $list[$key]; //用连接符,吧数组放到tree里面
}else{
//如果pid不是0,那就是二级栏目
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];//用pid去refer数组,匹配出父栏目数组,用连接符拼接到这里
$parent[$child][] =& $list[$key];aaa($parent);//将当前二级栏目放到父栏目下,拼接成二维数组
}
//仔细看list数组数据格式,第一遍key是0开始循环走的是tree哪里,
//第二遍从1开始走,走到parent这里,
}
}
}
return $tree;
}
$list = Array
(
[0] => Array
(
[id] => 112
[name] => /Admin
[title] => 后台
[type] => 1
[node_icon] =>
[class_icon] =>
[status] => 1
[condition] =>
[sort] => 0
[pid] => 0
[level] => 1
[update_time] => 1532081114
[create_time] => 1510821470
[is_display] => 1
)
[1] => Array
(
[id] => 117
[name] => /Admin/Index/index
[title] => 后台首页
[type] => 1
[node_icon] =>
[class_icon] =>
[status] => 1
[condition] =>
[sort] => 0
[pid] => 116
[level] => 3
[update_time] => 1510821714
[create_time] => 1510821714
[is_display] => 1
)
}
$refer = Array
(
[112] => Array
(
[id] => 112
[name] => /Admin
[title] => 后台
[type] => 1
[node_icon] =>
[class_icon] =>
[status] => 1
[condition] =>
[sort] => 0
[pid] => 0
[level] => 1
[update_time] => 1532081114
[create_time] => 1510821470
[is_display] => 1
)
[117] => Array
(
[id] => 117
[name] => /Admin/Index/index
[title] => 后台首页
[type] => 1
[node_icon] =>
[class_icon] =>
[status] => 1
[condition] =>
[sort] => 0
[pid] => 116
[level] => 3
[update_time] => 1510821714
[create_time] => 1510821714
[is_display] => 1
)
)
$tree = Array
(
[0] => Array
(
[id] => 112
[name] => /Admin
[title] => 后台
[type] => 1
[node_icon] =>
[class_icon] =>
[status] => 1
[condition] =>
[sort] => 0
[pid] => 0
[level] => 1
[update_time] => 1532081114
[create_time] => 1510821470
[is_display] => 1
)
)
$parent = Array
(
[id] => 116
[name] => /Admin/Index
[title] => 首页
[type] => 1
[node_icon] =>
[class_icon] =>
[status] => 1
[condition] =>
[sort] => 1
[pid] => 112
[level] => 2
[update_time] => 1510821685
[create_time] => 1510821685
[is_display] => 1
[is_status] => 1
[_child] => Array
(
[0] => Array
(
[id] => 117
[name] => /Admin/Index/index
[title] => 后台首页
[type] => 1
[node_icon] =>
[class_icon] =>
[status] => 1
[condition] =>
[sort] => 0
[pid] => 116
[level] => 3
[update_time] => 1510821714
[create_time] => 1510821714
[is_display] => 1
[is_status] => 1
)
)
)