树形导航菜单

原文出处http://renzhen.iteye.com/blog/1329949

想写树形导航菜单,然后各种百度,这里涉及到引用传递,写的蛮好的

/*
通过数据库获取所有元素,通过下面函数构造树形结构
*/
private function getTree($menus)
{
    	$id = $level = 0;
	$menuobjs=array();
    	$tree = array();
	$notrootmenu=array();
    	foreach($menus as $menu){
	        $menuobj=new stdClass();
		$menuobj->menu=$menu;
    		$id = $menu['id'];
		$level = $menu['father_id'];
		$menuobj->nodes = array();
		$menuobjs[$id]=$menuobj;
		if ($level) {
			$notrootmenu[]=$menuobj;
		} else {
			$tree[] = $menuobj;
		}
    	}

		foreach($notrootmenu as $menuobj){
			$menu=$menuobj->menu;
			$id = $menu['id'];
			$level = $menu['father_id'];
			$menuobjs[$level]->nodes[]=$menuobj;
		}
    	return $tree;
    }
}

 如果你想让自己的数组输出好看一些,可以做如下操作

 可以按照json格式输出数组

  echo "<pre>";print_r($arr);echo "<pre>";

 

整合一下我自己的输出

function setTree($tree){
	foreach ($tree as $key=>$val){
		$menu = $val->menu;
		$nodes = $val->nodes;
		$flag = str_repeat ("--",$menu["level"]);
		echo "<pre><font color=red>".$flag;print_r($menu["title"]);echo "</font></pre>";
		if(!is_null($nodes)){
			setTree($nodes);
		}
	}
}

 最后写一下数据格式

//数据输出
a
--c
----f
--d
b
--e
//数据格式
Array
(
    [0] => Nav Object
        (
            [menu] => Array
                (
                    [id] => 1
                    [title] => a
                    [url] => a
                    [father_id] => 0
                    [level] => 0
                )

            [nodes] => Array
                (
                    [0] => Nav Object
                        (
                            [menu] => Array
                                (
                                    [id] => 3
                                    [title] => c
                                    [url] => c
                                    [father_id] => 1
                                    [level] => 1
                                )

                            [nodes] => Array
                                (
                                    [0] => Nav Object
                                        (
                                            [menu] => Array
                                                (
                                                    [id] => 6
                                                    [title] => f
                                                    [url] => f
                                                    [father_id] => 3
                                                    [level] => 2
                                                )

                                            [nodes] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => Nav Object
                        (
                            [menu] => Array
                                (
                                    [id] => 4
                                    [title] => d
                                    [url] => d
                                    [father_id] => 1
                                    [level] => 1
                                )

                            [nodes] => Array
                                (
                                )

                        )

                )

        )

    [1] => Nav Object
        (
            [menu] => Array
                (
                    [id] => 2
                    [title] => b
                    [url] => b
                    [father_id] => 0
                    [level] => 0
                )

            [nodes] => Array
                (
                    [0] => Nav Object
                        (
                            [menu] => Array
                                (
                                    [id] => 5
                                    [title] => e
                                    [url] => e
                                    [father_id] => 2
                                    [level] => 1
                                )

                            [nodes] => Array
                                (
                                )

                        )

                )

        )

)

 

你可能感兴趣的:(导航菜单)