场景:从数据库中查询出来的数据,存在上下级关系,并且层级不确定,需要把查询出来的数据组织成tree结构。以下是示例代码
/**
* @param $data
* @param int $pid
* @return array
* 递归
* 题目-学生答题-tree
* author by liugong
*/
public function getRecursionHistoryChild($data, $pid = 0){
$childrenId_data = array();
if (empty($data)) {
return [];
}
foreach ($data as $key => $val) {
if ($val['question_pid'] == $pid) {
$item = [] ;
$item["question_id"] = $val['question_id'] ;
$item["score"] = $val['score'] ;
$item["child"] = $this ->getRecursionHistoryChild($data, $val['question_id']);
$childrenId_data[] = $item;
unset($data[$key]);
}
}
return $childrenId_data;
}