0
我想在一棵二叉樹中得到一個父親的所有下行線,每個父親有左右手臂,每個手臂有左右手臂等等。 like the following image。 在我的數據庫中,我有一張名爲users的表,每個用戶都有一個父親id和位置是L或R.得到上線的所有mlm下行線(php)
這是我的功能..但它仍然沒有得到所有下線。 like the following image。
我想在一棵二叉樹中得到一個父親的所有下行線,每個父親有左右手臂,每個手臂有左右手臂等等。 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
Stackoverflow不是一個編碼器租賃網站。如果您有具體問題,我們可以回答。 – Halcyon