最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php多级树形菜单代码
时间:2022-06-24 17:15:34 编辑:袖梨 来源:一聚教程网
数据库结果
打印如下:
代码如下 | 复制代码 |
array(7) { [0] => array(5) { ["id"] => string(1) "4" ["fzname"] => string(12) "山猫小号" ["userid"] => string(2) "12" ["pid"] => string(1) "0" ["time"] => string(10) "1413359977" } [1] => array(5) { ["id"] => string(1) "5" ["fzname"] => string(12) "山猫一组" ["userid"] => string(2) "12" ["pid"] => string(1) "4" ["time"] => string(10) "1413361206" } [2] => array(5) { ["id"] => string(1) "6" ["fzname"] => string(12) "山猫二组" ["userid"] => string(2) "12" ["pid"] => string(1) "4" ["time"] => string(10) "1413361225" } [3] => array(5) { ["id"] => string(1) "7" ["fzname"] => string(15) "山猫一组子" ["userid"] => string(2) "12" ["pid"] => string(1) "5" ["time"] => string(10) "1413361388" } [4] => array(5) { ["id"] => string(1) "8" ["fzname"] => string(15) "山猫一组子" ["userid"] => string(2) "12" ["pid"] => string(1) "5" ["time"] => string(10) "1413361409" } [5] => array(5) { ["id"] => string(1) "9" ["fzname"] => string(16) "山猫一组子3" ["userid"] => string(2) "12" ["pid"] => string(1) "5" ["time"] => string(10) "1413361460" } [6] => array(5) { ["id"] => string(2) "10" ["fzname"] => string(15) "山猫二组子" ["userid"] => string(2) "12" ["pid"] => string(1) "6" ["time"] => string(10) "1413361506" } } |
结果:
打印如下:
代码如下 | 复制代码 |
array(1) { [0] => array(5) { ["id"] => string(1) "4" ["fzname"] => string(12) "山猫小号" ["userid"] => string(2) "12" ["pid"] => array(2) { [0] => array(5) { ["id"] => string(1) "5" ["fzname"] => string(12) "山猫一组" ["userid"] => string(2) "12" ["pid"] => array(3) { [0] => array(5) { ["id"] => string(1) "7" ["fzname"] => string(15) "山猫一组子" ["userid"] => string(2) "12" ["pid"] => string(0) "" ["time"] => string(10) "1413361388" } [1] => array(5) { ["id"] => string(1) "8" ["fzname"] => string(15) "山猫一组子" ["userid"] => string(2) "12" ["pid"] => string(0) "" ["time"] => string(10) "1413361409" } [2] => array(5) { ["id"] => string(1) "9" ["fzname"] => string(16) "山猫一组子3" ["userid"] => string(2) "12" ["pid"] => string(0) "" ["time"] => string(10) "1413361460" } } ["time"] => string(10) "1413361206" } [1] => array(5) { ["id"] => string(1) "6" ["fzname"] => string(12) "山猫二组" ["userid"] => string(2) "12" ["pid"] => array(1) { [0] => array(5) { ["id"] => string(2) "10" ["fzname"] => string(15) "山猫二组子" ["userid"] => string(2) "12" ["pid"] => string(0) "" ["time"] => string(10) "1413361506" } } ["time"] => string(10) "1413361225" } } ["time"] => string(10) "1413359977" } } |
工用到两个函数1生成树形代码:
代码如下 | 复制代码 |
function getTree($data, $pId) { $tree = ''; foreach($data as $k => $v) { if($v['pid'] == $pId) { $v['pid'] = $this->getTree($data, $v['id']); $tree[] = $v; //unset($data[$k]); } } return $tree; } |
函数2转化为html代码:
代码如下 | 复制代码 |
function procHtml($tree) { $html = ''; foreach($tree as $t) { if($t['pid'] == '') { $html .= " } else { $html .= " $html .= $this->procHtml($t['pid']); $html = $html." } } return $html ? '
} |
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21