$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => '장시성'),
2 = > array('id' => 2, 'pid' => 0, 'name' => '헤이룽장성'),
3 => array('id' => 3, ' pid' => 1, '이름' => '난창시'),
4 => array('id' => 4, 'pid' => 2, '이름' => '하얼빈시'),
5 => array('id' => 5, 'pid' => 2, 'name' => '직시시'),
6 => array('id' => 6, 'pid' => 4, 'name' => '샹팡구'),
7 => array('id' => 7, 'pid' => 4, '이름' => '난강구'),
8 => array('id' => 8, 'pid' => 6, 'name' => ' 도로'),
9 => array('id' => 9, 'pid' => 7, 'name' => 'Xidazhi Street'),
10 = > 'id' => 10, 'pid' => 8, 'name' => '동북임업대학'),
11 => array('id' => 11, 'pid' = > 9, '이름' => '하얼빈 공과대학'),
12 => array('id' => 12, 'pid' => 8, '이름' = ' 하얼빈 사범대학교'),
13 => array('id' => 13, 'pid' => 1, 'name' => '간저우시'),
14 => array('id' => 14, 'pid' => 13, 'name' => '간군'),
15 => array('id' => 15, 'pid' => 13, '이름' => '유두현'),
16 => array('id' => 16, 'pid' => 14, '이름' = > '마오디엔' Town'),
17 => array('id' => 17, 'pid' => 14, 'name' => 'Datian Township'),
18 => 'id' => 18, 'pid' => 16, '이름' => '이위안 마을'),
19 => array('id' => 19 , 'pid' => ; 16, '이름' => '상바마을'),
)
방법 1
function genTree5($items) {
foreach ($items as $item)
$items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]] return isset($items [0]['son']) ? $items[0]['son'] : array();
}
방법 2:
함수 findChild ($arr,$id) {
$childs=array();
foreach ($arr as $k => $v){
if($v['pid']== $ id){
$ childs[]=$v;
}
function build_tree($root_id){
global $items;
$childs=findChild ($items,$root_id) ;
// print_r($childs);
// die()
if(empty($childs)){
return null;
foreach ($childs as $k => $v){
$rescurTree=build_tree($v['id'])
if( null != $rescurTree){
$ childs[$k][' son']=$rescurTree
} }
}
return $childs;