Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取上线的所有传销下线(php)_Php_Mysql_Binary Tree_Mlm - Fatal编程技术网

获取上线的所有传销下线(php)

获取上线的所有传销下线(php),php,mysql,binary-tree,mlm,Php,Mysql,Binary Tree,Mlm,我想得到一个二叉树中父亲的所有线条,每个父亲都有左臂和右臂,每个手臂都有左臂和右臂等等。。 . 在我的数据库中有一个名为users的表,每个用户都有一个父亲id和位置,即L或R 这是我的职责。。但它仍然没有得到所有的下线。 .我觉得有两件事很突出: $i参数和$this->downline\u id\u arr的使用 考虑做: $children = array(); foreach($data as $row) { $child_id = $row->id; $child

我想得到一个二叉树中父亲的所有线条,每个父亲都有左臂和右臂,每个手臂都有左臂和右臂等等。。 . 在我的数据库中有一个名为users的表,每个用户都有一个父亲id和位置,即L或R

这是我的职责。。但它仍然没有得到所有的下线。
.

我觉得有两件事很突出:

  • $i
    参数和
    $this->downline\u id\u 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\u id\u 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我需要一个php脚本来获取一个父亲的所有下线。我不是一个出租网站的编码员。如果你有一个特定的问题,我们可以回答。@Halcyon stackoverflow是互相帮助的,如果你想帮助,就去做,否则就把问题留给其他人来帮助。。我只需要将我的代码与其他想法进行比较。然后发布您的代码。