常用的无限极分类数据如下:

如何做树形格式
- 取出所有的分类信息
- 对分类信息进行树形遍历如下代码:
/**
* [tree 树结构]
* @param [type] $items [分类数据]
* @param [type] $pid [父Id]
* @return [type] [description]
*/
public function tree($items,$pid ="parent_id")
{
$map = [];
$tree = [];
//数据的ID名生成新的引用索引树
foreach ($items as &$it){
$map[$it['id']] = &$it;
}
foreach ($items as &$at){
$parent = &$map[$at[$pid]];
if($parent) {
$parent['children'][] = &$at;
}else{
$tree[] = &$at;
}
}
return $tree;
}
实际返回效果如下:
