PHP递归获取菜单类目

博客主要围绕PHP递归获取菜单类目展开,聚焦于利用PHP语言的递归方法来实现菜单类目的获取,属于后端开发中PHP技术的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#PHP递归获取菜单类目

	private function getChild($array,$pid = 0){
	    $data = array();
	    foreach ($array as $k=>$v){        //PID符合条件的
	        if($v['parent_id'] == $pid){            //寻找子集
	            $child = $this->getChild($array,$v['id']);            //加入数组
	            $v['child'] = $child?:array();
	            $data[] = $v;//加入数组中
	        }
	    }    return $data;
	}
	
	public function index(){
	    $this->load->model('virtual_phone_model');
        $res = $this->virtual_phone_model->get_menu();//获取数据库的全部菜单类目
        $res = $this->getChild($res);
        echo "<pre>";print_r($res);die;
	}
	/**
	打印数据
	Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 个人信息管理
            [url] => /personal_center/index
            [urls] => /personal_center/index|/personal_center/save|/personal_center/update_password|/personal_center/check_password
            [parent_id] => 0
            [sort] => 2
            [icon] => iF-user
            [child] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 4
            [name] => 用户管理
            [url] => /shop/staff/index
            [urls] => /shop/staff/index|/shop/staff/add|/shop/staff/edit|/shop/staff/save|/shop/staff/delete|/shop/staff/edit_password|/shop/staff/set_status|/shop/staff/check_staff|/shop/staff/check_form
            [parent_id] => 0
            [sort] => 3
            [icon] => 
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 21
                            [name] => 角色管理
                            [url] => /shop/role/index
                            [urls] => /shop/role/index|/shop/role/add|/shop/role/edit|/shop/role/save|/shop/role/delete
                            [parent_id] => 4
                            [sort] => 6
                            [icon] => 
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 20
            [name] => 操作日志
            [url] => /shop_staff_log/index
            [urls] => /shop_staff_log/index
            [parent_id] => 0
            [sort] => 12
            [icon] => iF-logs
            [child] => Array
                (
                )

        )

    [3] => Array
        (
            [id] => 23
            [name] => 首页
            [url] => /home/index
            [urls] => /home/index
            [parent_id] => 0
            [sort] => 1
            [icon] => 
            [child] => Array
                (
                )

        )

    [4] => Array
        (
            [id] => 28
            [name] => 数据统计
            [url] => /report/order/index
            [urls] => /report/order/index
            [parent_id] => 0
            [sort] => 14
            [icon] => iF-report
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 18
                            [name] => 通话记录
                            [url] => /report/call/index
                            [urls] => /report/call/index
                            [parent_id] => 28
                            [sort] => 10
                            [icon] => 
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 19
                            [name] => 充值记录
                            [url] => /report/fee_log/index
                            [urls] => /report/fee_log/index
                            [parent_id] => 28
                            [sort] => 11
                            [icon] => 
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [5] => Array
        (
            [id] => 30
            [name] => 虚拟号码管理
            [url] => /virtual_phone/index
            [urls] => /virtual_phone/index|/virtual_phone/add|/virtual_phone/edit|/virtual_phone/save|/virtual_phone/delete|/virtual_phone/check_phone|/virtual_phone/upload_exl
            [parent_id] => 0
            [sort] => 0
            [icon] => 
            [child] => Array
                (
                )

        )

)
	**/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值