1 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 2 <?php 3 $tree=array( 4 array('id'=>1,'name'=>'安徽','pid'=>'0'), 5 array('id'=>2,'name'=>'北京','pid'=>'0'), 6 array('id'=>3,'name'=>'山东','pid'=>'0'), 7 array('id'=>4,'name'=>'昭阳区','pid'=>'2'), 8 array('id'=>5,'name'=>'合肥市','pid'=>'1'), 9 array('id'=>6,'name'=>'蜀山区','pid'=>'5'), 10 array('id'=>7,'name'=>'烟台市','pid'=>'3'), 11 ); 12 /** 13 * @param $data array 14 * @param $pid parent_id 15 * @param $level 16 * @author tian 17 */ 18 function tree($data,$pid=0,$level=1){ 19 static $arr=array(); 20 foreach($data as $v){ 21 if($v['pid']==$pid){ 22 $v['level']=$level; 23 $arr[]=$v; 24 tree($data,$v['id'],$level+1); 25 } 26 } 27 return $arr; 28 } 29 30 $list=tree($tree,0,1); 31 var_dump($list); 32 ?>