Rumah > Soal Jawab > teks badan
Cara melakukan klasifikasi tanpa had berdasarkan id dan pid
迷茫2017-06-05 11:10:01
Gunakan fungsi rekursif, tetapi kerumitan ruang kaedah ini tidak dioptimumkan Saya terlupa cara menulisnya sebelum ini
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;
}
phpcn_u15822017-06-05 11:10:01
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;
}