'1', "caption" => "一级菜单1"), &"/> '1', "caption" => "一级菜单1"), &">
Heim >Backend-Entwicklung >PHP-Tutorial > PHP数组分级,百般无奈,救助
PHP数组分级,万般无奈,救助
array (
array("level" => '1', "caption" => "一级菜单1"),
array("leve1" => '1', "caption" => "一级菜单2"),
array("leve1" => '2', "caption" => "二级菜单1"),
array("leve1" => '2', "caption" => "二级菜单2"),
array("leve1" => '1', "caption" => "一级菜单3"),
)
其中level = 2为其上方最近leve1 = 1 的子菜单,不排除会有level = 3的子菜单(从属于level = 2)现在想弄成下面的形式,想了好久也没什么好方法,同事建议我用递归,可递归小弟不太懂,PHP初接触,给各位大拿添麻烦了
array (
array("level" => '1', "caption" => "一级菜单1"),
array("leve1" => '1', "caption" => "一级菜单2",
"sub"=>array("leve1" => '2', "caption" => "二级菜单1"),
"sub"=>array("leve1" => '2', "caption" => "二级菜单2")
),
array("leve1" => '1', "caption" => "一级菜单3"),
)
<br> <br> <br> $arr = array(<br> <br> array('id'=>1,'city_name'=>'中国','rel_id'=>'1','pid'=>0),<br> array('id'=>2,'city_name'=>'广东','rel_id'=>'1-2','pid'=>1),<br> array('id'=>3,'city_name'=>'深圳','rel_id'=>'1-2-3','pid'=>2),<br> array('id'=>4,'city_name'=>'广州','rel_id'=>'1-2-4','pid'=>2)<br> <br> );<br> <br> function find_subclass( $pid ){<br> <br> global $arr;<br> $__arr = array();<br> foreach ( $arr as $k=>$v )<br> {<br> <br> if( $v['pid']==$pid )$__arr[] = $v;<br> <br> }<br> return $__arr;<br> <br> }<br> <br> function tree_subclass($pid=0){<br> <br> $__arr = array();<br> $__arr = find_subclass($pid);<br> if( !empty($__arr) ){<br> <br> foreach ( $__arr as $k=>$v )<br> { <div class="clear"> </div>