-
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
栏目列表
最新更新
vbs能调用的系统对象小结
vbscript网页模拟登录效果代码
VBScript 根据IE窗口的标题输出ESC
杀死指定进程名称的小VBS
通过vbs修改以点结尾的文件的属性为隐藏
查询电脑开关机时间的vbs代码
VBA中的Timer函数用法
ComboBox 控件的用法教程
在windows 64位操作系统上运行32位的vbscri
无法执行vbs脚本中遇到的问题及解决方案
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比