根据引用实现索引树(无限极分类)
$array = array(
0=>array('id'=>1,'uid'=>0,'menuname'=>'菜单1','url'=>0,'addtime'=>'2018-08-29 16:36:55'),
1=>array('id'=>2,'uid'=>0,'menuname'=>'菜单2','url'=>0,'addtime'=>'2018-08-29 16:36:55'),
2=>array('id'=>3,'uid'=>1,'menuname'=>'1子菜单1','url'=>0,'addtime'=>'2018-08-29 16:36:55'),
3=>array('id'=>4,'uid'=>3,'menuname'=>'1子菜单2','url'=>0,'addtime'=>'2018-08-29 16:36:55'),
4=>array('id'=>5,'uid'=>2,'menuname'=>'2子菜单1','url'=>0,'addtime'=>'2018-08-29 16:36:55'),
);
function getTree($items,$pid ="uid") {
$map= [];
$tree = [];
foreach ($items as &$it) $map[$it['id']] = &$it;
//数据的ID名生成新的引用索引树
foreach ($items as $k => &$at){
$parent = &$map[$at[$pid]];
if($parent) {
$parent['children'][] = &$at;
}else{
$tree[] = &$at;
}
}
return $tree;
}print_r(getTree($array));
die;
出来的效果是
Array
(
[0] => Array
(
[id] => 1
[uid] => 0
[menuname] => 菜单1
[url] => 0
[addtime] => 2018-08-29 16:36:55
[children] => Array
(
[0] => Array
(
[id] => 3
[uid] => 1
[menuname] => 1子菜单1
[url] => 0
[addtime] => 2018-08-29 16:36:55
[children] => Array
(
[0] => Array
(
[id] => 4
[uid] => 3
[menuname] => 1子菜单2
[url] => 0
[addtime] => 2018-08-29 16:36:55
)))))[1] => Array
(
[id] => 2
[uid] => 0
[menuname] => 菜单2
[url] => 0
[addtime] => 2018-08-29 16:36:55
[children] => Array
(
[0] => Array
(
[id] => 5
[uid] => 2
[menuname] => 2子菜单1
[url] => 0
[addtime] => 2018-08-29 16:36:55
))))
【根据引用实现索引树(无限极分类)】转载于:https://my.oschina.net/u/3482593/blog/3076232
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- 解决SpringBoot引用别的模块无法注入的问题
- java中如何实现重建二叉树
- 人脸识别|【人脸识别系列】| 实现自动化妆
- paddle|动手从头实现LSTM