码迷,mamicode.com
首页 > Web开发 > 详细

【PHP原生】递归实现无限级分类树状展示数据

时间:2018-10-16 13:42:00      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:树状   主键   存在   数据库设计   bsp   str   row   string   去掉   

主要应用领域:无限极分类的菜单栏、对评论进行无限次追评 ...

1、数据库设计要求:每一条记录都需要存在id(主键)和pid(父用户id)字段。
2、查询数据库获取所有的记录组成的数组。
3、递归组合成新的数组
//封装成类里面的方法
//$data表示所有的记录组成的数组。&寻址增加查询效率。
public function getTree(&$data, $parentId = 0)
{
$tree = [];
foreach($data as $k => $v)
{
if($v[‘pid‘] == $parentId)
{ //父亲找到儿子
$v[‘child‘] = self::getTree($data, $v[‘id‘]); //封装成函数的时候,需要去掉self::这个标识。  
$tree[] = $v;
unset($data[$k]); // 释放原始数据变量
}
}
return $tree;
}
4、显示结果举例
排序前:
array(8) {
[0] => array(6) {
["id"] => int(1)
["memb_id"] => int(4)
["content"] => string(13) "回复内容1"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(0)
}
[1] => array(6) {
["id"] => int(2)
["memb_id"] => int(4)
["content"] => string(13) "回复内容2"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(0)
}
[2] => array(6) {
["id"] => int(3)
["memb_id"] => int(4)
["content"] => string(13) "回复内容3"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(0)
}
[3] => array(6) {
["id"] => int(4)
["memb_id"] => int(4)
["content"] => string(13) "回复内容4"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(1)
}
[4] => array(6) {
["id"] => int(5)
["memb_id"] => int(4)
["content"] => string(13) "回复内容5"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(4)
}
[5] => array(6) {
["id"] => int(6)
["memb_id"] => int(4)
["content"] => string(13) "回复内容6"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(4)
}
[6] => array(6) {
["id"] => int(7)
["memb_id"] => int(4)
["content"] => string(13) "回复内容7"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(5)
}
[7] => array(6) {
["id"] => int(8)
["memb_id"] => int(4)
["content"] => string(13) "回复内容8"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(6)
}
}
排序后:
array(3) {
[0] => array(7) {
["id"] => int(1)
["memb_id"] => int(4)
["content"] => string(13) "回复内容1"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(0)
["child"] => array(1) {
[0] => array(7) {
["id"] => int(4)
["memb_id"] => int(4)
["content"] => string(13) "回复内容4"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(1)
["child"] => array(2) {
[0] => array(7) {
["id"] => int(5)
["memb_id"] => int(4)
["content"] => string(13) "回复内容5"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(4)
["child"] => array(1) {
[0] => array(7) {
["id"] => int(7)
["memb_id"] => int(4)
["content"] => string(13) "回复内容7"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(5)
["child"] => array(0) {}}}
}
[1] => array(7) {
["id"] => int(6)
["memb_id"] => int(4)
["content"] => string(13) "回复内容6"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(4)
["child"] => array(1) {
[0] => array(7) {
["id"] => int(8)
["memb_id"] => int(4)
["content"] => string(13) "回复内容8"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(6)
["child"] => array(0) {}}}}}}}}
[1] => array(7) {
["id"] => int(2)
["memb_id"] => int(4)
["content"] => string(13) "回复内容2"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(0)
["child"] => array(0) {}
}
[2] => array(7) {
["id"] => int(3)
["memb_id"] => int(4)
["content"] => string(13) "回复内容3"
["add_time"] => int(1500622839)
["ask_id"] => int(1)
["pid"] => int(0)
["child"] => array(0) {}}
}

 

 

【PHP原生】递归实现无限级分类树状展示数据

标签:树状   主键   存在   数据库设计   bsp   str   row   string   去掉   

原文地址:https://www.cnblogs.com/php99/p/9796969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!