$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => 'Provinz Jiangxi'),
2 = > array('id' => 2, 'pid' => 0, 'name' => 'Provinz Heilongjiang'),
3 => 3, ' pid' => 1, 'name' =>
4 => 4, 'pid' => 2, 'name' => 'Harbin City'),
5 => array('id' => 5, 'pid' => 2, 'name' => 'Jixi City'),
6 => array('id' => 6, 'pid' => 4, 'name' => 'Xiangfang District'),
7 => array('id' => 7, 'pid ' => 4, 'name' => 'Nangang District'),
8 => array('id' => 6, 'name' => ' Hexing Road'),
9 => array('id' => 7, 'name' => 'Xidazhi Street'),
10 = > 'id' => 10, 'pid' => 8, 'name' => 'Northeast Forestry University'),
11 => 11, 'pid' = > 9, 'name' => 'Harbin Institute of Technology'),
12 => 12, 'pid' => 8, 'name' = > Harbin Normal University'),
13 => array('id' => 13, 'pid' => 1, 'name' => 'Ganzhou City'),
14 => array('id' => 14, 'pid' => 13, 'name' => 'Gan County'),
15 => array('id' => 15, 'pid' => 13, 'name' => 'Yudu County'),
16 => 16, 'pid' => 'Maodian Town'),
17 => array('id' => 14, 'name' => 'Datian Township'),
18 => 'id' => 18, 'pid' => 16, 'name' => ; 16, 'name' => 'Shangba Village'),
Methode 1;
function genTree5($items) {
foreach ($items as $item)
$items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; [0]['son']) ? $items[0]['son'] : array();
}
Methode 2:
function findChild ($arr,$id) {
$childs=array();
foreach ($arr as $k => $v){
if($v['pid']== $ id){
$ childs[]=$v;
}
function build_tree($root_id){
$childs =array(
$childs=findChild ($items,$root_id) ;
// print_r($childs);
if(empty($childs)){
return null;
foreach ($childs as $k => $v){
$rescurTree=build_tree($v['id']);
if( null != $rescurTree){
$ childs[$k]['son']=$rescurTree;
} }
}
return $childs;