三级数组查询
$data = db::name('AuthRule')->select();
$data = list_to_tree2($data,'id','pid');//aaa($data);
// halt($data);
$this->assign("nodeList", $data);
return $this->fetch();
/** * 把返回的数据集转换成Tree * @access public * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * @param string $level level标记字段 * @return array */ function list_to_tree2($list, $pk='id',$pid = 'pid',$child = '_child',$root=0) { // 创建Tree $tree = array(); if(is_array($list)) { // 创建基于主键的数组引用 $refer = array(); foreach ($list as $key => $data) { $refer[$data[$pk]] =& $list[$key];//取出每个栏目id作为键名, } foreach ($list as $key => $data) {//开始循环,key从0开始一个一个往下走0123.。。 // 判断是否存在parent $parentId = $data[$pid];//echo $parentId.'|';//0|112|116|117|112输出每个数组里的pid if ($root == $parentId) {//key从0开始循环,如果是一级栏目pid==0 $tree[] =& $list[$key]; //用连接符,吧数组放到tree里面 }else{ //如果pid不是0,那就是二级栏目 if (isset($refer[$parentId])) { $parent =& $refer[$parentId];//用pid去refer数组,匹配出父栏目数组,用连接符拼接到这里 $parent[$child][] =& $list[$key];aaa($parent);//将当前二级栏目放到父栏目下,拼接成二维数组 } //仔细看list数组数据格式,第一遍key是0开始循环走的是tree哪里, //第二遍从1开始走,走到parent这里, } } } return $tree; }
打印出的数据格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | $list = Array ( [0] => Array ( [id] => 112 [name] => /Admin [title] => 后台 [type] => 1 [node_icon] => [class_icon] => [status] => 1 [condition] => [sort] => 0 [pid] => 0 [level] => 1 [update_time] => 1532081114 [create_time] => 1510821470 [is_display] => 1 ) [1] => Array ( [id] => 117 [name] => /Admin/Index/index [title] => 后台首页 [type] => 1 [node_icon] => [class_icon] => [status] => 1 [condition] => [sort] => 0 [pid] => 116 [level] => 3 [update_time] => 1510821714 [create_time] => 1510821714 [is_display] => 1 ) } $refer = Array ( [112] => Array ( [id] => 112 [name] => /Admin [title] => 后台 [type] => 1 [node_icon] => [class_icon] => [status] => 1 [condition] => [sort] => 0 [pid] => 0 [level] => 1 [update_time] => 1532081114 [create_time] => 1510821470 [is_display] => 1 ) [117] => Array ( [id] => 117 [name] => /Admin/Index/index [title] => 后台首页 [type] => 1 [node_icon] => [class_icon] => [status] => 1 [condition] => [sort] => 0 [pid] => 116 [level] => 3 [update_time] => 1510821714 [create_time] => 1510821714 [is_display] => 1 ) ) $tree = Array ( [0] => Array ( [id] => 112 [name] => /Admin [title] => 后台 [type] => 1 [node_icon] => [class_icon] => [status] => 1 [condition] => [sort] => 0 [pid] => 0 [level] => 1 [update_time] => 1532081114 [create_time] => 1510821470 [is_display] => 1 ) ) $parent = Array ( [id] => 116 [name] => /Admin/Index [title] => 首页 [type] => 1 [node_icon] => [class_icon] => [status] => 1 [condition] => [sort] => 1 [pid] => 112 [level] => 2 [update_time] => 1510821685 [create_time] => 1510821685 [is_display] => 1 [is_status] => 1 [_child] => Array ( [0] => Array ( [id] => 117 [name] => /Admin/Index/index [title] => 后台首页 [type] => 1 [node_icon] => [class_icon] => [status] => 1 [condition] => [sort] => 0 [pid] => 116 [level] => 3 [update_time] => 1510821714 [create_time] => 1510821714 [is_display] => 1 [is_status] => 1 ) ) ) |
页面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | < dt >管理权限</ dt > < dd > < table width="100%" border="0" cellspacing="0" cellpadding="0" class="tab rules-data" data-rules="{present name ="$info['rules']"}{$info['rules']}{/present}"> {foreach name="nodeList" item="level1"} < tr > < td style="font-size: 14px;">< label >< input name="rules[]" level="1" type="checkbox" obj="node_{$level1.id}" value="{$level1.id}" data="{$level1.id}:1:0" data-id="{$level1.id}" data-pid="0"/> < b class="mac-size">[项目]</ b > {$level1.title}</ label ></ td > </ tr > {present name="level1['_child']"} {foreach name="level1['_child']" item="level2" } < tr > < td style="padding-left:60px; font-size: 14px;">< label >< input name="rules[]" level="2" type="checkbox" obj="node_{$level1.id}_{$level2.id}" value="{$level2.id}" data="{$level2.id}:2:{$level2.pid}" data-id="{$level2.id}" data-pid="{$level2.pid}"/> < b class="mac-size">[模块]</ b > {$level2.title}</ label ></ td > </ tr > {present name="$level2['_child']"} {volist name="level2['_child']" id="level3"} < tr >< td style="padding-left: 120px;"> < label >< input name="rules[]" level="3" type="checkbox" obj="node_{$level1.id}_{$level2.id}_{$level3.id}" value="{$level3.id}" data="{$level3.id}:3:{$level3.pid}" data-id="{$level3.id}" data-pid="{$level3.pid}"/> < b class="mac-size">[操作]</ b >{$level3.title}</ label > </ td ></ tr > {present name="$level3['_child']"} < tr > < td style="padding-left: 160px;"> {foreach name="level3['_child']" item="level4" key="k" } {if condition="$level4.is_display eq 1"}| {/if} < label > < input name="rules[]" level="4" type="checkbox" obj="node_{$level1.id}_{$level2.id}_{$level3.id}_{$level4.id}" value="{$level4.id}" data="{$level4.id}:4:{$level4.pid}" data-id="{$level4.id}" data-pid="{$level4.pid}"/> {$level4.title} </ label > {/foreach} </ td > </ tr > {/present} {/volist} {/present} {/foreach} {/present} {/foreach} |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?