二分木で父親のすべてのダウンラインを取得したい、各父親には左腕と右腕があり、各腕には左腕と右腕があります .次の画像のように. 私のデータベースにはusersというテーブルがあり、各ユーザーには父親のIDとLまたはRの位置があります。
これが私の機能です..しかし、それでもすべてのダウンラインを取得するわけではありません。 次の画像のように。
私には2つのことが際立っています:
$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
これで、変数 orは必要なくなりました$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;
}
通常、クエリが少ないほど高速になるため、パフォーマンスが向上するはずです。