Created
May 25, 2020 06:14
-
-
Save Jason-cqtan/40e50def4ccb5b9ace0c96f1fb0bf056 to your computer and use it in GitHub Desktop.
父子关系列表转树形结构
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 创建树形结构 | |
* @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