Skip to content

Instantly share code, notes, and snippets.

@Jason-cqtan
Created May 25, 2020 06:14
Show Gist options
  • Save Jason-cqtan/40e50def4ccb5b9ace0c96f1fb0bf056 to your computer and use it in GitHub Desktop.
Save Jason-cqtan/40e50def4ccb5b9ace0c96f1fb0bf056 to your computer and use it in GitHub Desktop.
父子关系列表转树形结构
/**
* 创建树形结构
* @param [type] $data [description]
* @param [type] $parent_id [description]
* @return [type] [description]
*/
public function getTree($data,$parent_id){
$result= array();//结果数组
$len = 0;
for($i=0;$i<count($data);$i++){
if($data[$i]['parent_id'] === $parent_id){
$result[$len] = $data[$i];
$children = $this->getTree($data,$data[$i]['id']);
if(count($children)>0){
$result[$len]['children ']= $children;
}
$len++;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment