如何在二叉树中检索和显示父节点的所有子节点

2fjabf4q  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(263)
public function get_all_downlines($id){
    $tree = array();
    $this->db->where('sponsor_id', $id);
    $query = $this->db->get('users');
    $children =  $query->result_array();
    foreach($children as $child){
        $tree[] = $this->get_all_downlines($child['id']) ;
    }
    return $tree;
}

我使用的是邻接列表,有像id,id和position这样的列。我能用上面的代码输出的是空数组。我希望能够以一种建议层次结构的方式输出数组。我需要你的帮助好人。我已经绞尽脑汁好几天了。对于一个有6条下线的父级,我在array([0]=>array([0]=>array()[1]=>array())[1]=>array[0]=>array()[1]=>array())下面得到了这样的结果
我的更新代码

public function get_all_downlines($id){
  $tree = array();
   $this->db->where('sponsor_id', $id);
  $query = $this->db->get('users');
  $children =  $query->result_array();
     foreach($children as $child){
        $tree[$child['id']] = $this->get_all_downlines($child['id']);
     }
     return $tree;

}
dfuffjeb

dfuffjeb1#

public function getAllComponentTree($id =''){
		$tree = array();
		$this->db->select('id, scheme_component_name, parent_id');
		$this->db->from('mst_scheme_component');
		if(!empty($id)){
   			$this->db->where('parent_id', $id);
		}else{
			$this->db->where('parent_id is NULL');
		}
  		$query = $this->db->get();
  		$children =  $query->result_array();

     	foreach($children as $child){
     		$data = $this->getAllComponentTree($child['id']);
     		if(!empty($data)){
        		$tree[] = $child;
        		$tree[$child['id']] = $this->getAllComponentTree($child['id']);
     		}else{
     			$tree[] = $child;
     		}
     	}
     	return $tree;
	}
[0] => Array
        (
            [id] => 1
            [scheme_component_name] => test Data
            [parent_id] => 
        )
[1] => Array
    (
        [0] => Array
            (
                [id] => 59
                [scheme_component_name] =>test Data
                [parent_id] => 1
            )

        [1] => Array
            (
                [id] => 60
                [scheme_component_name] => test Data
                [parent_id] => 1
            )

        [60] => Array
            (
                [0] => Array
                    (
                        [id] => 62
                        [scheme_component_name] => test Data
                        [parent_id] => 60
                    )

                [62] => Array
                    (
                        [0] => Array
                            (
                                [id] => 63
                                [scheme_component_name] => test Data
                                [parent_id] => 62
                            )

                    )

            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 61
                [scheme_component_name] => test Data
                [parent_id] => 2
            )

    )

[3] => Array
    (
        [id] => 3
        [scheme_component_name] => test Data
        [parent_id] => 
    )

[4] => Array
    (
        [id] => 4
        [scheme_component_name] => test Data
        [parent_id] => 
    )

[5] => Array
    (
        [id] => 5
        [scheme_component_name] => test Data
        [parent_id] => 
    )

[6] => Array
    (
        [id] => 6
        [scheme_component_name] => test Data
        [parent_id] => 
    )

相关问题