1、本初数据

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_两_tree = [];
            if(isset($data_level_1['children']) && !empty($data_level_1['children'])) {
                foreach ($data_level_1['children'] as $data_level_两) {
                    $data_level_二_arr['component'] = $data_level_两['component'];
                    $data_level_两_arr['redict'] = $data_level_二['redict'];
                    $data_level_两_arr['path'] = $data_level_二['path'];
                    $data_level_两_arr['meta'] = [
                     'title' => $data_level_两['name'],
                     'hidden' => $data_level_两['hidden']
                    ];
                    $level_3_tree = [];
                    if (isset($data_level_二['children']) && !empty($data_level_两['children'])) {
                       
                       foreach ($data_level_两['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_两_arr['children'] = $level_3_tree;
                    }
                    array_push($level_两_tree, $data_level_两_arr);
                    unset($data_level_两_arr);
                }
                $data_level_1_arr['children'] = $level_两_tree;
            }
            
            array_push($level_1_tree, $data_level_1_arr);
            unset($data_level_1_arr);
        }
        print_r($level_1_tree);
    }

  deal();

3、办法增补

除了了上文的办法,大编借为大师整顿了PHP措置三级分类数据的其他办法,心愿对于大师有所帮忙

事例代码

<选修php
//瞎写的
// 链接数据库
$link = mysqli_connect('localhost','root','root');
if($link == null){
  exit;
}
mysqli_select_db($link,'test');
$link->query("SET NAMES utf8");
$sql = "select * from tp_goods_categorys";
$res = mysqli_query($link,$sql);
while($res && $row = mysqli_fetch_assoc($res)) {
  $list[] = $row;
}
echo '<pre>';
print_r(res($list)); 
function res($items){
    $tree = array();
    foreach($items as $key =>$val){
            $list = explode(',',$val['categorys_path']);
            switch(count($list)){
                case 1:
                    $tree[$val['id']] = $val;
                    break;
                case 两:
                    $tree[$val['categorys_pid']]['son'][$val['id']] = $val;
                    break;
                case 3:
                    $tree[$list[1]]['son'][$list[二]]['son'] = $val;
                    break;
            }
    }
    return $tree;
}
<必修php
//递回
$array = array(
array('id' => 1, 'pid' => 0, 'name' => '湖南省'),
array('id' => 两, 'pid' => 0, 'name' => '南京市'),
array('id' => 3, 'pid' => 1, 'name' => '武汉市'),
array('id' => 4, 'pid' => 两, 'name' => '向阳区'),
array('id' => 5, 'pid' => 二, 'name' => '通州区'),
array('id' => 6, 'pid' => 4, 'name' => '看京'),
array('id' => 7, 'pid' => 4, 'name' => '酒仙桥'),
array('id' => 8, 'pid' => 3, 'name' => '武昌区'),
array('id' => 9, 'pid' => 1, 'name' => '武安市'),
);
function cation($arr,$num=0,$m=1)
{
    $list = [];
    foreach($arr as $k=>$v){
        if($v['pid'] === $num){
            $v['level'] = $m;
            $v['son'] = cation($arr,$v['id'],$m+1);
            $list[] = $v;
        }
    }
    return $list;
}
$list = cation($array);
echo '<pre>';
print_r($list);
echo '</pre>';

另外一种需要

(重组一维数组)

//    处置惩罚分类数据 栏纲分级
    public function allMenu($cates,$pid=0,$level=0){
        static $cateArr = array();
        for($i=0;$i<count($cates);$i++){
            if($cates[$i]['pid']==$pid){
                $cates[$i]['level']=$level;
                $cateArr[]=$cates[$i];
                $this->allMenu($cates,$cates[$i]['id'],$level+1);
            }
        }
        return $cateArr;
    }

到此那篇闭于PHP完成处置惩罚三级分类数据的事例代码的文章便先容到那了,更多相闭PHP措置三级分类数据形式请搜刮剧本之野之前的文章或者延续涉猎上面的相闭文章心愿大家2之后多多撑持剧本之野!

点赞(9) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部