我想在一棵二叉树中获取父亲的所有下线,每个父亲都有左臂和右臂,每个臂都有左臂和右臂等等。 like the following image 。 在我的数据库中,我有一个名为 users 的表,每个用户都有一个父亲 id 和位置,即 L 或 R。
这是我的功能..但它仍然没有得到所有下线。 like the following image .
最佳答案
有两件事对我来说很突出:
$i
参数以及$this->downline_id_arr
的使用。
考虑这样做:
$children = array();
foreach($data as $row) {
$child_id = $row->id;
$children[$child_id] = array(/**/);
$children = array_merge($children, $this->getAllDownline($child_id);
}
return $childen;
现在您不需要 $i
变量或 $this->downline_id_arr
。
- 您正在逐一查询每个节点。
考虑按级别查询:
function getAllDownlines($fathers) {
$data = "SELECT * FROM users WHERE father_id IN (/*fathers*/)";
$new_father_ids = array();
$children = array();
foreach ($data as $child) {
$children[$child->id] = array(/**/); // etc
$new_father_ids[] = $child->id;
}
$children = array_merge($children, $this->getAllDownlines($new_father_ids);
return $childen;
}
通常,查询越少,速度就越快,因此您应该会看到更好的性能。
关于php - 获取上线的所有传销下线 (php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303705/