得到上线的所有mlm下行线(php)
问题描述:
我想在一棵二叉树中得到一个父亲的所有下行线,每个父亲有左右手臂,每个手臂有左右手臂等等。 like the following image。 在我的数据库中,我有一张名为users的表,每个用户都有一个父亲id和位置是L或R.得到上线的所有mlm下行线(php)
这是我的功能..但它仍然没有得到所有下线。 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;
}
通常较少查询的速度更快,所以你会看到更好的性能。
这听起来像是一个非常标准的算法问题。我们能帮到你什么? – Halcyon
@Halcyon我需要一个PHP脚本来获得父亲的所有下线id – Hamdy
*不是一个编码器租赁网站。如果您有具体问题,我们可以回答。 – Halcyon