<?phpheader("Content-Type:text/html;charset=utf-8");$aarr = array(array('id'=>1,'name'=>'安徽','pid'=>0),array('id'=>2,'name'=>'海淀','pid'=>7),array('id'=>3,'name'=>'浣溪县','pid'=>5),array('id'=>4,'name'=>'昌平','pid'=>7),array('id'=>5,'name'=>'淮北','pid'=>1),array('id'=>6,'name'=>'朝阳','pid'=>7),array('id'=>7,'name'=>'北京','pid'=>0),array('id'=>8,'name'=>'上地','pid'=>2));//家谱树的应用,如面包屑导航,首页 > 手机类型 > GSM手机 > iPhone > iPhone6sfunction familytree($arr,$id){$tree = array();foreach($arr as $v){if($v['id'] == $id){if($v['pid'] > 0){$tree = array_merge($tree,familytree($arr,$v['pid']));}$tree[] = $v; //这里换下顺序北京就可以上前面去了 }}return $tree;}echo "<pre>";print_r(familytree($aarr,8)); //上一课输出的顺序是:上海->海淀->北京