0,'val'=>'顶级菜单1','parent'=>-1);$mixTree[] = array('id'=>1,'val'=>'顶级菜单2','parent'=>-1);$mixTree[] = array('id'=>2,'val'=>'顶级菜单1下的二级菜单1','parent'=>0);$mixTree[] = array('id'=>3,'val'=>'顶级菜单1下的二级菜单2','parent'=>0);$mixTree[] = array('id'=>4,'val'=>'顶级菜单2下的二级菜单1','parent'=>1);$mixTree[] = array('id'=>5,'val'=>'顶级菜单1下的二级菜单2下的三级菜单1','parent'=>3);/* * 通过二维数组把上面的数据挂载成树 */$treeDatas = array();foreach($mixTree as $nodeDatas){ if($nodeDatas['parent']==-1){ $treeDatas[-1][]=$nodeDatas; }else{ $treeDatas[$nodeDatas['parent']][]=$nodeDatas; }}/* * 遍历打印其父结点及其子结点 */function traverSesing($nodeDatas,$treeDatas){ echo $nodeDatas['val']; if(!empty($treeDatas[$nodeDatas['id']])&& is_array($treeDatas[$nodeDatas['id']])){ echo '
- '; foreach($treeDatas[$nodeDatas['id']] as $childNodeDatas){ echo '
- '; traverSesing($childNodeDatas, $treeDatas); echo ' '; } echo '
- ';foreach($treeDatas[-1] as $nodeDatas){ echo '
- '; traverSesing($nodeDatas, $treeDatas); echo ' ';}echo '
执行结果如图: