从给定的父ID PHP中查找所有子节点和孙节点

时间:2016-09-21 13:16:06

标签: php

我在推荐系统工作。 让我们说用户A邀请用户B和用户C.然后用户B邀请用户D,用户C邀请用户E,依此类推。我如何遍历所有子节点和孙节点?

以下是我的数据库中的示例数据

function sample(obj){
  var key;
  for (key in obj) {
    console.log(key + ": " + obj[key]);
  }
  
  console.log("Direct access - i: " + obj.i);
  console.log("Direct access - e: " + obj.e);
}

var foo = "this is foo";
var bar = "this is bar";
sample({i:foo,e:bar});

如何创建一个功能,将用户A作为父级检索,然后列出用户B(带子节点D)和用户C(带子节点E)??

我正在使用CI,这是我从父ID

中检索2个子节点的查询
ID       name       referral_id
1        user A     null//assume the tree starts from here
2        user B     1
3        user C     1
4        user D     2
5        user E     3

我不知道在这一步之后要去哪里。已经完成了几种类型的算法,包括while循环和foreach循环。但它仍然没有"动态"。

请注意,我只需要所有子孙的名字和身份证明......它不一定是多维数组。只要我能让所有的孩子和granchildren和曾孙子已经好了..

我打算将所有子孙后代等保存到$ downline数组中。所有都将在没有多维数组的1个数组中

真的需要有关如何做到这一点的指导..以前从来没有完成过这个层次结构..谢谢你:)

2 个答案:

答案 0 :(得分:0)

这应该这样做,假设$downline_query包含您需要的所有行。 IE 1 DB查询。

$affilites = [];
$parents = []
foreach($downline_query as $row){
   if( $row['referral_id'] ){
       if( !isset(  $affilites[$row['referral_id']] ) ){
             $affilites[$row['referral_id']] = [];
       }
       $affilites[$row['referral_id']][] = $row;
   }else{
       $parents[$row['id']] = $row;
   }
}
// you now have 2 arrays 1 of parents, one of children. now just combine ( shown below )
$parent = [
    1 => ['ID'=>1,'name'=>'user A', 'referral_id' =>null],  
];

$affiliate = [
    1 => [
            ['ID'=>2,'name'=>'user B', 'referral_id' =>1],
            ['ID'=>3,'name'=>'user C', 'referral_id' =>1],
        ],
    2 => [['ID'=>4,'name'=>'user D', 'referral_id' =>2]],
    3 => [['ID'=>5,'name'=>'user E', 'referral_id' =>3]],
    5 => [['ID'=>16,'name'=>'user F', 'referral_id' =>5]],
];


print_r(getparents( $parent, $affiliate));

function getparents( $parent, $affiliate ){
    $result = [];
    foreach ($parent as $row ){
        $id = $row['ID'];

        $result[] = $row;

        if( isset($affiliate[$id]) ){
         //no children
            $result[$id]['children'] = getparents( $affiliate[$id], $affiliate ); //recurse
        }
    }
    return $result;
}

最终输出

 Array(
        [0] => Array(
                [ID] => 1
                [name] => user A
                [referral_id] =>
                [children] => Array(
                        [0] => Array(
                                [ID] => 2
                                [name] => user B
                                [referral_id] => 1
                                [children] => Array(
                                        [0] => Array(
                                                [ID] => 4
                                                [name] => user D
                                                [referral_id] => 2
                                                )

                                        )

                                )

                        [1] => Array(
                                [ID] => 3
                                [name] => user C
                                [referral_id] => 1
                                [children] => Array(
                                        [0] => Array(
                                                [ID] => 5
                                                [name] => user E
                                                [referral_id] => 3
                                                [children] => Array(
                                                        [0] => Array(
                                                                [ID] => 16
                                                                [name] => user F
                                                                [referral_id] => 5
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        ) 

答案 1 :(得分:-1)

您可以尝试递归函数:

function get_referrals ($referral_id){
    $downline = array();
    $downline_query = $this->db->query("SELECT * FROM user WHERE referral_id = '$referral_id'");
    foreach($downline_query as $result){
       $downline[] = array(
           "name" => $result['name'],
           "id"   => $result['id']
       );
       $downline['invited'] = get_referrals($result['id']);  # <--- recursive !!!
    }
    return $downline;
}

获取$ downline

$downline = get_referrals(1);


print_r($downline);