第一种方法:
function recursionHandleData($data)
{
//第一步 构造数据
$items = array();
foreach($data as $value){
$items[$value['id']] = $value;
}
//第二部 遍历数据 生成树状结构
$tree = array();
foreach($items as $key => $value){
if(isset($items[$value['pid']])){
$items[$value['pid']]['children'][] = &$items[$key];
}else{
$tree[] = &$items[$key];
}
}
return $tree;
}
第二种方法:
function recursionHandleData($data, $pid = 0)
{
$child = [];
foreach ($data as $value) {
if ($value['pid'] == $pid) {
$child[$value['id']] = $value;
$child[$value['id']]['children'] = recursionHandleData($data, $value['id']);
}
}
return $child;
}
|