作者:花落---守护者 | 来源:互联网 | 2018-03-23 10:22
数据表字段,id,parentid。
父分类parentid=0,子分类的parentid=父id。
function getDataTree($rows, $id='id',$pid = 'parentid',$child = 'child',$root=0) { $tree = array(); // 树
if(is_array($rows)){
$array = array();
foreach ($rows as $key=>$item){
$array[$item[$id]] =& $rows[$key];
}
foreach($rows as $key=>$item){
$parentId = $item[$pid];
if($root == $parentId){
$tree[] =&$rows[$key];
}else{
if(isset($array[$parentId])){
$parent =&$array[$parentId];
$parent[$child][]=&$rows[$key];
}
}
}
}
return $tree;
}