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

时间:2022-10-24 17:37:32

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

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) {}} }