VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP实现处理三级分类数据的示例代码

这篇文章主要为大家详细介绍了如何利用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();

三、方法补充
除了上文的方法,小编还为大家整理了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 2:
                    $tree[$val['categorys_pid']]['son'][$val['id']] = $val;
                    break;
                case 3:
                    $tree[$list[1]]['son'][$list[2]]['son'] = $val;
                    break;
            }
    }
    return $tree;
}
<?php
//递归
$array = array(
array('id' => 1, 'pid' => 0, 'name' => '湖北省'),
array('id' => 2, 'pid' => 0, 'name' => '北京市'),
array('id' => 3, 'pid' => 1, 'name' => '武汉市'),
array('id' => 4, 'pid' => 2, 'name' => '朝阳区'),
array('id' => 5, 'pid' => 2, '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处理三级分类数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持

原文链接:https://blog.csdn.net/bianenhui91/article/details/136024791


相关教程