php递归问题: 5
public$arr=array();publicfunctionget_child($id,$level){foreach($this->arras$value){if...
public $arr = array();
public function get_child($id,$level){
foreach($this->arr as $value){
if($value['parentid'] == $id){
$value['level'] = $level;
$child[$value['id']] = $value;
}
}
return $child;
}
public function menu($id=0,$level=1){//默认从顶级菜单开始
$child = $this->get_child($id,$level);
if(is_array($child)){
foreach($child as $key=>$value){
$menu_arr[$value['id']] = $value;
$this->menu($value['id'],$level);
$level++;
}
}
return $menu_arr;
}
$this->menu($value['id'],$level);
这一句去掉的效果跟不去掉是一样的,但是期望的是实现递归
Array ( [1] => Array ( [id] => 1 [name] => 我的面板 [parentid] => 0 [level] => 1 ) [2] => Array ( [id] => 2 [name] => 站点配置 [parentid] => 0 [level] => 1 ) [3] => Array ( [id] => 3 [name] => 内容管理 [parentid] => 0 [level] => 1 ) [4] => Array ( [id] => 4 [name] => 管理员管理 [parentid] => 0 [level] => 1 ) [5] => Array ( [id] => 5 [name] => 会员管理 [parentid] => 0 [level] => 1 ) [6] => Array ( [id] => 6 [name] => 功能管理 [parentid] => 0 [level] => 1 ) )
这是结果,但是想用递归算法将其他记录也能存入$menu_arr数组中 展开
public function get_child($id,$level){
foreach($this->arr as $value){
if($value['parentid'] == $id){
$value['level'] = $level;
$child[$value['id']] = $value;
}
}
return $child;
}
public function menu($id=0,$level=1){//默认从顶级菜单开始
$child = $this->get_child($id,$level);
if(is_array($child)){
foreach($child as $key=>$value){
$menu_arr[$value['id']] = $value;
$this->menu($value['id'],$level);
$level++;
}
}
return $menu_arr;
}
$this->menu($value['id'],$level);
这一句去掉的效果跟不去掉是一样的,但是期望的是实现递归
Array ( [1] => Array ( [id] => 1 [name] => 我的面板 [parentid] => 0 [level] => 1 ) [2] => Array ( [id] => 2 [name] => 站点配置 [parentid] => 0 [level] => 1 ) [3] => Array ( [id] => 3 [name] => 内容管理 [parentid] => 0 [level] => 1 ) [4] => Array ( [id] => 4 [name] => 管理员管理 [parentid] => 0 [level] => 1 ) [5] => Array ( [id] => 5 [name] => 会员管理 [parentid] => 0 [level] => 1 ) [6] => Array ( [id] => 6 [name] => 功能管理 [parentid] => 0 [level] => 1 ) )
这是结果,但是想用递归算法将其他记录也能存入$menu_arr数组中 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询