标签:树状 主键 存在 数据库设计 bsp str row string 去掉
//封装成类里面的方法
//$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;
}
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) {}}
}
标签:树状 主键 存在 数据库设计 bsp str row string 去掉
原文地址:https://www.cnblogs.com/php99/p/9796969.html