Array (
[0] => Array
(
[id] => 2
[pid] => 0
[create_time] => 1626248460
[update_time] => 0
[source_name] => 三方
[department_number] =>
[hid] => 1
)
[1] => Array
(
[id] => 3
[pid] => 0
[create_time] => 1626248631
[update_time] => 0
[source_name] => 竞价
[department_number] =>
[hid] => 1
)
[2] => Array
(
[id] => 4
[pid] => 0
[create_time] => 1626248641
[update_time] => 0
[source_name] => 新媒体
[department_number] =>
[hid] => 1
)
[3] => Array
(
[id] => 5
[pid] => 0
[create_time] => 1626249029
[update_time] => 1626249489
[source_name] => 电商1
[department_number] =>
[hid] => 1
)
[4] => Array
(
[id] => 6
[pid] => 5
[create_time] => 1626249957
[update_time] => 1626250184
[source_name] => 电商23
[department_number] =>
[hid] => 1
)
[5] => Array
(
[id] => 8
[pid] => 4
[create_time] => 1626250215
[update_time] => 0
[source_name] => 新媒体1
[department_number] =>
[hid] => 1
)
[6] => Array
(
[id] => 9
[pid] => 5
[create_time] => 1626251341
[update_time] => 0
[source_name] => 电商33
[department_number] =>
[hid] => 1
)
[7] => Array
(
[id] => 10
[pid] => 5
[create_time] => 1626251347
[update_time] => 0
[source_name] => 电商44
[department_number] =>
[hid] => 1
)
[8] => Array
(
[id] => 11
[pid] => 3
[create_time] => 1626312001
[update_time] => 0
[source_name] => 竞价竞价竞
[department_number] =>
[hid] => 1
)
[9] => Array
(
[id] => 13
[pid] => 0
[create_time] => 1626678565
[update_time] => 0
[source_name] => 渠道来源
[department_number] =>
[hid] => 1
)
[10] => Array
(
[id] => 14
[pid] => 13
[create_time] => 1626678574
[update_time] => 0
[source_name] => 渠道来源子类
[department_number] =>
[hid] => 1
)
)
非递归实现 //第一步,将分类id作为数组key,并创建children单元
$tree = array();
foreach($channelLists as $category){
$tree[$category['id']] = $category;
$tree[$category['id']]['children'] = array();
}
//第二步,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。
foreach($tree as $key=>$item){
if($item['pid'] != 0){
$tree[$item['pid']]['children'][] = &$tree[$key];
if($tree[$key]['children'] == null){
unset($tree[$key]['children']);
}
}
}
//第三步,删除无用的非根节点数据
foreach($tree as $key=>$category){
if($category['pid'] != 0){
unset($tree[$key]);
}
}
echo "<pre>";
print_r($tree);
echo "<pre>";exit;
运行结果如下 Array (
[2] => Array
(
[id] => 2
[pid] => 0
[create_time] => 1626248460
[update_time] => 0
[source_name] => 三方
[department_number] =>
[hid] => 1
[children] => Array
(
)
)
[3] => Array
(
[id] => 3
[pid] => 0
[create_time] => 1626248631
[update_time] => 0
[source_name] => 竞价
[department_number] =>
[hid] => 1
[children] => Array
(
[0] => Array
(
[id] => 11
[pid] => 3
[create_time] => 1626312001
[update_time] => 0
[source_name] => 竞价竞价竞
[department_number] =>
[hid] => 1
)
)
)
[4] => Array
(
[id] => 4
[pid] => 0
[create_time] => 1626248641
[update_time] => 0
[source_name] => 新媒体
[department_number] =>
[hid] => 1
[children] => Array
(
[0] => Array
(
[id] => 8
[pid] => 4
[create_time] => 1626250215
[update_time] => 0
[source_name] => 新媒体1
[department_number] =>
[hid] => 1
)
)
)
[5] => Array
(
[id] => 5
[pid] => 0
[create_time] => 1626249029
[update_time] => 1626249489
[source_name] => 电商1
[department_number] =>
[hid] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[pid] => 5
[create_time] => 1626249957
[update_time] => 1626250184
[source_name] => 电商23
[department_number] =>
[hid] => 1
)
[1] => Array
(
[id] => 9
[pid] => 5
[create_time] => 1626251341
[update_time] => 0
[source_name] => 电商33
[department_number] =>
[hid] => 1
)
[2] => Array
(
[id] => 10
[pid] => 5
[create_time] => 1626251347
[update_time] => 0
[source_name] => 电商44
[department_number] =>
[hid] => 1
)
)
)
[13] => Array
(
[id] => 13
[pid] => 0
[create_time] => 1626678565
[update_time] => 0
[source_name] => 渠道来源
[department_number] =>
[hid] => 1
[children] => Array
(
[0] => Array
(
[id] => 14
[pid] => 13
[create_time] => 1626678574
[update_time] => 0
[source_name] => 渠道来源子类
[department_number] =>
[hid] => 1
)
)
)
)
引用算法
$items = array();
foreach($channelLists as $v){
$items[$v['id']] = $v;
}
$tree = array();
foreach($items as $k => $item){
if(isset($items[$item['pid']])){
$items[$item['pid']]['son'][] = &$items[$k];
}else{
$tree[] = &$items[$k];
}
}
echo "<pre>";
print_r($tree);
echo "<pre>";exit;
运行结果如下 Array (
[0] => Array
(
[id] => 2
[pid] => 0
[create_time] => 1626248460
[update_time] => 0
[source_name] => 三方
[department_number] =>
[hid] => 1
)
[1] => Array
(
[id] => 3
[pid] => 0
[create_time] => 1626248631
[update_time] => 0
[source_name] => 竞价
[department_number] =>
[hid] => 1
[son] => Array
(
[0] => Array
(
[id] => 11
[pid] => 3
[create_time] => 1626312001
[update_time] => 0
[source_name] => 竞价竞价竞
[department_number] =>
[hid] => 1
)
)
)
[2] => Array
(
[id] => 4
[pid] => 0
[create_time] => 1626248641
[update_time] => 0
[source_name] => 新媒体
[department_number] =>
[hid] => 1
[son] => Array
(
[0] => Array
(
[id] => 8
[pid] => 4
[create_time] => 1626250215
[update_time] => 0
[source_name] => 新媒体1
[department_number] =>
[hid] => 1
)
)
)
[3] => Array
(
[id] => 5
[pid] => 0
[create_time] => 1626249029
[update_time] => 1626249489
[source_name] => 电商1
[department_number] =>
[hid] => 1
[son] => Array
(
[0] => Array
(
[id] => 6
[pid] => 5
[create_time] => 1626249957
[update_time] => 1626250184
[source_name] => 电商23
[department_number] =>
[hid] => 1
)
[1] => Array
(
[id] => 9
[pid] => 5
[create_time] => 1626251341
[update_time] => 0
[source_name] => 电商33
[department_number] =>
[hid] => 1
)
[2] => Array
(
[id] => 10
[pid] => 5
[create_time] => 1626251347
[update_time] => 0
[source_name] => 电商44
[department_number] =>
[hid] => 1
)
)
)
[4] => Array
(
[id] => 13
[pid] => 0
[create_time] => 1626678565
[update_time] => 0
[source_name] => 渠道来源
[department_number] =>
[hid] => 1
[son] => Array
(
[0] => Array
(
[id] => 14
[pid] => 13
[create_time] => 1626678574
[update_time] => 0
[source_name] => 渠道来源子类
[department_number] =>
[hid] => 1
)
)
)
)
|