您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了根据引用日均实现索引树的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
$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 ) ) ) )
感谢你能够认真阅读完这篇文章,希望小编分享的“根据引用日均实现索引树的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。