本文介绍: 【代码】PHP三级分类数据处理。

一、原始数据

function getDatas(){
    return [
	[
		'component' => 'system',
		'redict' => '/system',
		'path' => '/system',
		'hidden' => 1,
		'name' => '系统管理',
		'children' => [
			[
				'component' => 'user',
				'redict' => '/user',
				'path' => '/user',
				'hidden' => 1,
				'name' => '用户管理',
				'children' => [
					[
						'component' => 'user_list',
						'redict' => '/user_list',
						'path' => '/user_list',
						'hidden' => 1,
						'name' => '用户列表',
					],
					[
						'component' => 'role',
						'redict' => '/user_role',
						'path' => '/user_role',
						'hidden' => 1,
						'name' => '角色列表',
					],
					[
						'component' => 'perm',
						'redict' => '/perm',
						'path' => '/perm',
						'hidden' => 1,
						'name' => '权限列表',
					],
				]
			],
			[
				'component' => 'data',
				'redict' => '/data',
				'path' => '/data',
				'hidden' => 1,
				'name' => '数据管理',
				'children' => [
					[
						'component' => 'report',
						'redict' => '/report',
						'path' => '/report',
						'hidden' => 1,
						'name' => '分日报表',
					],
					[
						'component' => 'hour_report',
						'redict' => '/hour_report',
						'path' => '/hour_report',
						'hidden' => 1,
						'name' => '分时报表',
					],
					[
						'component' => 'month_report',
						'redict' => '/month_report',
						'path' => '/month_report',
						'hidden' => 1,
						'name' => '分月报表',
					],
				]
			],
			[
				'component' => 'resource',
				'redict' => '/resource',
				'path' => '/resource',
				'hidden' => 1,
				'name' => '资源管理',
				'children' => [
					[
						'component' => 'res_list',
						'redict' => '/res_list',
						'path' => '/res_list',
						'hidden' => 1,
						'name' => '资源列表'
					]
				]
			]
		]
	]
]
}

二、函数处理

function deal(){
        $datas = getDatas();
        $level_1_tree = [];
        foreach($datas as $data_level_1){
            $data_level_1_arr['component'] = $data_level_1['component'];
            $data_level_1_arr['redict'] = $data_level_1['redict'];
            $data_level_1_arr['path'] = $data_level_1['path'];
            $data_level_1_arr['meta'] = [
                 'title' => $data_level_1['name'],
                 'hidden' => $data_level_1['hidden']
            ];
            $level_2_tree = [];
            if(isset($data_level_1['children']) && !empty($data_level_1['children'])) {
                foreach ($data_level_1['children'] as $data_level_2) {
                    $data_level_2_arr['component'] = $data_level_2['component'];
                    $data_level_2_arr['redict'] = $data_level_2['redict'];
                    $data_level_2_arr['path'] = $data_level_2['path'];
                    $data_level_2_arr['meta'] = [
                     'title' => $data_level_2['name'],
                     'hidden' => $data_level_2['hidden']
                    ];
                    $level_3_tree = [];
                    if (isset($data_level_2['children']) && !empty($data_level_2['children'])) {
                       
                       foreach ($data_level_2['children'] as $data_level_3) {
                            $data_level_3_arr['component'] = $data_level_3['component'];
                            $data_level_3_arr['redict'] = $data_level_3['redict'];
                            $data_level_3_arr['path'] = $data_level_3['path'];
                            $data_level_3_arr['meta'] = [
                               'title' => $data_level_3['name'],
                               'hidden' => $data_level_3['hidden']
                            ];
                            array_push($level_3_tree, $data_level_3_arr);
                            unset($data_level_3_arr);
                       }
                       $data_level_2_arr['children'] = $level_3_tree;
                    }
                    array_push($level_2_tree, $data_level_2_arr);
                    unset($data_level_2_arr);
                }
                $data_level_1_arr['children'] = $level_2_tree;
            }
            
            array_push($level_1_tree, $data_level_1_arr);
            unset($data_level_1_arr);
        }
        print_r($level_1_tree);
    }

  deal();


原文地址:https://blog.csdn.net/bianenhui91/article/details/136024791

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_67889.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注