场景
需要获取数据表的菜单数据,并转换成树状结构返回给前端
方案
function list_to_tree($list, $parent_id = 0) {
$tree = array();
foreach ($list as $item) {
if ($item['pid'] == $parent_id) {
$children = list_to_tree($list, $item['id']);
if (!empty($children)) {
$item['children'] = $children;
}
$tree[] = $item;
}
}
return $tree;
}