How to perform unlimited classification based on id and pid
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
Use a recursive function, but the space complexity of this method was not optimized. I forgot the previous way of writing it
private function getTreeList($data, $pid = 0) { $resultarr = array(); foreach ($data as $teamdata) { if ($teamdata['pid'] == $pid) { $team_data = $teamdata; $children_data = $this->getTreeList($data, $teamdata['id']); $team_data['children'] = $children_data; $resultarr[] = $team_data; } } return $resultarr; }
Use recursion to loop out an array
public function gettree($items, $parent_id = 'parent_id', $id = 'id'){ $tree = array(); //格式化好的树 if(empty($items)){ return $tree; } foreach ($items as $item){ if (isset($items[$item[$parent_id]])){ $items[$item[$parent_id]]['son'][] = &$items[$item[$id]]; }else{ $tree[] = &$items[$item[$id]]; } } return $tree; }
Use a recursive function, but the space complexity of this method was not optimized. I forgot the previous way of writing it
Use recursion to loop out an array