数据结构和方法如下:
$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'),
2 => array('id' => 2, 'pid' => 0, 'name' => '黑龙江省'),
3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'),
4 => array('id' => 4, 'pid' => 2, 'name' => '哈尔滨市'),
5 => array('id' => 5, 'pid' => 2, 'name' => '鸡西市'),
6 => array('id' => 6, 'pid' => 4, 'name' => '香坊区'),
7 => array('id' => 7, 'pid' => 4, 'name' => '南岗区'),
8 => array('id' => 8, 'pid' => 6, 'name' => '和兴路'),
9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'),
10 => array('id' => 10, 'pid' => 8, 'name' => '东北林业大学'),
11 => array('id' => 11, 'pid' => 9, 'name' => '哈尔滨工业大学'),
12 => array('id' => 12, 'pid' => 8, 'name' => '哈尔滨师范大学'),
13 => array('id' => 13, 'pid' => 1, 'name' => '赣州市'),
14 => array('id' => 14, 'pid' => 13, 'name' => '赣县'),
15 => array('id' => 15, 'pid' => 13, 'name' => '于都县'),
16 => array('id' => 16, 'pid' => 14, 'name' => '茅店镇'),
17 => array('id' => 17, 'pid' => 14, 'name' => '大田乡'),
18 => array('id' => 18, 'pid' => 16, 'name' => '义源村'),
19 => array('id' => 19, 'pid' => 16, 'name' => '上坝村'),
);
function tree($items){
$tree = array(); //初始化树结构数据
foreach($items as $value){
if(isset($items[$value['pid']])){ //如果当前父级数据存在
$items[$value['pid']]['son'][] = &$items[$value['id']]; //在对应父级元素添加 son 元素数据,添加的数据为当前数据
}else{ //如果当前父级数据不存在
$tree[] = &$items[$value['id']]; //将当前数据添加到树结构数据里
}
}
return $tree;
}
function genTree($items) {
foreach ($items as $item)
$items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; //与上方法一样,只是键值不一样
return isset($items[0]['son']) ? $items[0]['son'] : array();
}
print_r(tree($items));
print_r(genTree($items));
返回结果如下:
Array
(
[0] => Array
(
[id] => 1
[pid] => 0
[name] => 江西省
[son] => Array
(
[0] => Array
(
[id] => 3
[pid] => 1
[name] => 南昌市
)
[1] => Array
(
[id] => 13
[pid] => 1
[name] => 赣州市
[son] => Array
(
[0] => Array
(
[id] => 14
[pid] => 13
[name] => 赣县
[son] => Array
(
[0] => Array
(
[id] => 16
[pid] => 14
[name] => 茅店镇
[son] => Array
(
[0] => Array
(
[id] => 18
[pid] => 16
[name] => 义源村
)
[1] => Array
(
[id] => 19
[pid] => 16
[name] => 上坝村
)
)
)
[1] => Array
(
[id] => 17
[pid] => 14
[name] => 大田乡
)
)
)
[1] => Array
(
[id] => 15
[pid] => 13
[name] => 于都县
)
)
)
)
)
[1] => Array
(
[id] => 2
[pid] => 0
[name] => 黑龙江省
[son] => Array
(
[0] => Array
(
[id] => 4
[pid] => 2
[name] => 哈尔滨市
[son] => Array
(
[0] => Array
(
[id] => 6
[pid] => 4
[name] => 香坊区
[son] => Array
(
[0] => Array
(
[id] => 8
[pid] => 6
[name] => 和兴路
[son] => Array
(
[0] => Array
(
[id] => 10
[pid] => 8
[name] => 东北林业大学
)
[1] => Array
(
[id] => 12
[pid] => 8
[name] => 哈尔滨师范大学
)
)
)
)
)
[1] => Array
(
[id] => 7
[pid] => 4
[name] => 南岗区
[son] => Array
(
[0] => Array
(
[id] => 9
[pid] => 7
[name] => 西大直街
[son] => Array
(
[0] => Array
(
[id] => 11
[pid] => 9
[name] => 哈尔滨工业大学
)
)
)
)
)
)
)
[1] => Array
(
[id] => 5
[pid] => 2
[name] => 鸡西市
)
)
)
)
Array
(
[1] => Array
(
[id] => 1
[pid] => 0
[name] => 江西省
[son] => Array
(
[3] => Array
(
[id] => 3
[pid] => 1
[name] => 南昌市
)
[13] => Array
(
[id] => 13
[pid] => 1
[name] => 赣州市
[son] => Array
(
[14] => Array
(
[id] => 14
[pid] => 13
[name] => 赣县
[son] => Array
(
[16] => Array
(
[id] => 16
[pid] => 14
[name] => 茅店镇
[son] => Array
(
[18] => Array
(
[id] => 18
[pid] => 16
[name] => 义源村
)
[19] => Array
(
[id] => 19
[pid] => 16
[name] => 上坝村
)
)
)
[17] => Array
(
[id] => 17
[pid] => 14
[name] => 大田乡
)
)
)
[15] => Array
(
[id] => 15
[pid] => 13
[name] => 于都县
)
)
)
)
)
[2] => Array
(
[id] => 2
[pid] => 0
[name] => 黑龙江省
[son] => Array
(
[4] => Array
(
[id] => 4
[pid] => 2
[name] => 哈尔滨市
[son] => Array
(
[6] => Array
(
[id] => 6
[pid] => 4
[name] => 香坊区
[son] => Array
(
[8] => Array
(
[id] => 8
[pid] => 6
[name] => 和兴路
[son] => Array
(
[10] => Array
(
[id] => 10
[pid] => 8
[name] => 东北林业大学
)
[12] => Array
(
[id] => 12
[pid] => 8
[name] => 哈尔滨师范大学
)
)
)
)
)
[7] => Array
(
[id] => 7
[pid] => 4
[name] => 南岗区
[son] => Array
(
[9] => Array
(
[id] => 9
[pid] => 7
[name] => 西大直街
[son] => Array
(
[11] => Array
(
[id] => 11
[pid] => 9
[name] => 哈尔滨工业大学
)
)
)
)
)
)
)
[5] => Array
(
[id] => 5
[pid] => 2
[name] => 鸡西市
)
)
)
)
参考:https://www.cnblogs.com/zzg521/p/6761576.html
测试数据:
$arrCate = array( //待排序数组
array('id' => 1, 'name' => '顶级栏目一', 'parent_id' => 0),
array('id' => 2, 'name' => '顶级栏目二', 'parent_id' => 0),
array('id' => 3, 'name' => '栏目三', 'parent_id' => 1),
array('id' => 4, 'name' => '栏目四', 'parent_id' => 3),
array('id' => 5, 'name' => '栏目五', 'parent_id' => 4),
array('id' => 6, 'name' => '栏目六', 'parent_id' => 2),
array('id' => 7, 'name' => '栏目七', 'parent_id' => 6),
array('id' => 8, 'name' => '栏目八', 'parent_id' => 6),
array('id' => 9, 'name' => '栏目九', 'parent_id' => 7),
);
获取对应父级的所有子级,返回扁平化结构数据
/**
* 递归无限级分类,获取任意节点下所有子孩子
* @param array $arrCate 待排序的数组
* @param int $parent_id 父级节点
* @param int $level 层级数
* @return array $arrTree 排序后的数组(扁平化数据)
*/
function getTree($arrCate, $parent_id = 0, $level = 0){
static $arrTree = []; //static函数执行完后变量值仍然保存
if(empty($arrCate)) return [];
$level++;
foreach($arrCate as $key => $value){
if($value['parent_id'] == $parent_id){
$value['level'] = $level;
$arrTree[] = $value;
unset($arrCate[$key]); //注销当前节点数据,减少已无用的遍历
getTree($arrCate, $value['id'], $level); //递归调用
}
}
return $arrTree;
}
print_r(getTree($arrCate));
//输出结果如下:
Array
(
[0] => Array
(
[id] => 1
[name] => 顶级栏目一
[parent_id] => 0
[level] => 1
)
[1] => Array
(
[id] => 3
[name] => 栏目三
[parent_id] => 1
[level] => 2
)
[2] => Array
(
[id] => 4
[name] => 栏目四
[parent_id] => 3
[level] => 3
)
[3] => Array
(
[id] => 5
[name] => 栏目五
[parent_id] => 4
[level] => 4
)
[4] => Array
(
[id] => 2
[name] => 顶级栏目二
[parent_id] => 0
[level] => 1
)
[5] => Array
(
[id] => 6
[name] => 栏目六
[parent_id] => 2
[level] => 2
)
[6] => Array
(
[id] => 7
[name] => 栏目七
[parent_id] => 6
[level] => 3
)
[7] => Array
(
[id] => 9
[name] => 栏目九
[parent_id] => 7
[level] => 4
)
[8] => Array
(
[id] => 8
[name] => 栏目八
[parent_id] => 6
[level] => 3
)
)
获取对应父级的所有子级,返回树形结构数据
/**
* 把二维数组集转换成Tree
* @param $list 数据集
* @param string $pk 主键
* @param string $pid 父级id
* @param string $child 子级数据键
* @param int $root 指定顶级id
* @return array $tree 处理后的数据(树形结构)
*/
function listTree($list, $pk = 'id', $pid = 'parent_id', $child = 'children', $root = 0){
$tree = [];
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;
}
print_r(listTree($arrCate));
//输出结果如下:
Array
(
[0] => Array
(
[id] => 1
[name] => 顶级栏目一
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 3
[name] => 栏目三
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 4
[name] => 栏目四
[parent_id] => 3
[children] => Array
(
[0] => Array
(
[id] => 5
[name] => 栏目五
[parent_id] => 4
)
)
)
)
)
)
)
[1] => Array
(
[id] => 2
[name] => 顶级栏目二
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 6
[name] => 栏目六
[parent_id] => 2
[children] => Array
(
[0] => Array
(
[id] => 7
[name] => 栏目七
[parent_id] => 6
[children] => Array
(
[0] => Array
(
[id] => 9
[name] => 栏目九
[parent_id] => 7
)
)
)
[1] => Array
(
[id] => 8
[name] => 栏目八
[parent_id] => 6
)
)
)
)
)
)
测试数据:
$arrCate = array( //待排序数组
array('id' => 1, 'name' => '顶级栏目一', 'parent_id' => 0),
array('id' => 2, 'name' => '顶级栏目二', 'parent_id' => 0),
array('id' => 3, 'name' => '栏目三', 'parent_id' => 1),
array('id' => 4, 'name' => '栏目四', 'parent_id' => 3),
array('id' => 5, 'name' => '栏目五', 'parent_id' => 4),
array('id' => 6, 'name' => '栏目六', 'parent_id' => 2),
array('id' => 7, 'name' => '栏目七', 'parent_id' => 6),
array('id' => 8, 'name' => '栏目八', 'parent_id' => 6),
array('id' => 9, 'name' => '栏目九', 'parent_id' => 7),
);
找到所有父级数据(扁平化结构):
/**
* 递归无限级分类,获取任意节点的所有父级
* @param array $arrCate 待排序的数组
* @param int $child_id 子级节点
* @return array $children 排序后的数组(扁平化数据)
*/
function getparent($arrCate, $child_id = 9){
static $children = [];
foreach($arrCate as $key => $value){
if($value['id'] == $child_id){
$children[$value['id']] = $value;
getparent($arrCate,$value['parent_id']);
}
}
return $children;
}
print_r(getparent($arrCate));
//输出示例
Array
(
[9] => Array
(
[id] => 9
[name] => 栏目九
[parent_id] => 7
)
[7] => Array
(
[id] => 7
[name] => 栏目七
[parent_id] => 6
)
[6] => Array
(
[id] => 6
[name] => 栏目六
[parent_id] => 2
)
[2] => Array
(
[id] => 2
[name] => 顶级栏目二
[parent_id] => 0
)
)
找到所有父级数据(树形结构):
/**
* 递归无限级分类,获取任意节点的所有父级
* @param array $arrCate 待排序的数组
* @param int $child_id 子级节点
* @return array $children 排序后的数组
*/
function getparents($arrCate, $child_id = 9){
static $children = [];
foreach($arrCate as $key => $value){
if($value['id'] == $child_id){
$children[$value['id']] = $value;
getparents($arrCate,$value['parent_id']);
}
}
foreach($children as $k => $v){
if($v['parent_id'] != 0){
$children[$v['parent_id']]['children'] = $v; //将子级拼接进父级的children里
}
if($v['parent_id'] == 0){
$top_id = $v['id']; //最顶级节点id
}
}
return $children[$top_id]; //返回最顶级到指定子级节点的树形结构数据
}
print_r(getparents($arrCate));
//示例输出:
Array
(
[id] => 2
[name] => 顶级栏目二
[parent_id] => 0
[children] => Array
(
[id] => 6
[name] => 栏目六
[parent_id] => 2
[children] => Array
(
[id] => 7
[name] => 栏目七
[parent_id] => 6
[children] => Array
(
[id] => 9
[name] => 栏目九
[parent_id] => 7
)
)
)
)