从递归函数中获取数组子函数



我有变量与子数据库。例如:

$roots = $this->getCategoryTable()->getCategoryRoot($project_id);//themes
            if($roots->count()> 0){
                foreach($roots as $root){
                    $root_id = $root->id;
                    //show name root
                    echo $root->name;
                    //show children of root
                    if($this->getCategoryTable()->checkHasRowsChildren($root_id)){
                        $rootChild = $this->getCategoryTable()->getRowsChildren($root_id);
                        if($rootChild->count() > 0){
                            foreach($rootChild as $key => $val) {
                                $array_themes[]=$val->name;
                                $result = $this->getChild($val->id, $array_themes);
                            }//end of foreach child of root
                        }//end of existing child of root 
                    }
                }//end of foreach root
            }//end of existing root 

这是我获取子元素的函数

function getChild($root_id, $array_themes) {
    if($this->getCategoryTable()->checkHasRowsChildren($root_id)){
        $rootChild = $this->getCategoryTable()->getRowsChildren($root_id);
        if($rootChild->count() > 0){
            foreach($rootChild as $key => $val) {
                $array_themes[]=$val->name;
                $this->getChild($val->id, $array_themes);
            }//end of foreach child of root
        }//end of existing child of root 
    }
}

和结果应该是这样的:

Child: Parent

H: G
G: D
J: C
F: C
C: A
B: A
A: E
E: D
D: Null

我得到了所有的父母和孩子。但在最终结果中我想要得到result数组,比如

  $array_result = array(
        [0]=>"D",
        [1]=>"E",
        [2]=>"A",
        [3]=>"C",
        [4]=>"F",
        [5]=>"J",
        [6]=>"G",
        [7]=>"H"
  );

它是可能得到数组的结果吗?

我认为你很接近解决方案,你只需要添加谁是当前子的父,(嵌套foreach的变化):

$roots = $this->getCategoryTable()->getCategoryRoot($project_id);//themes
if($roots->count()> 0){
    foreach($roots as $root){
        $root_id = $root->id;
        //show name root
        echo $root->name;
        //show children of root
        if($this->getCategoryTable()->checkHasRowsChildren($root_id)){
            $rootChild = $this->getCategoryTable()->getRowsChildren($root_id);
            if($rootChild->count() > 0){
                foreach($rootChild as $key => $val) {
                    //CHANGES IN HERE
                    //SINCE THIS IS THE TOP, THERE ARE NO PARENT ( = NULL)
                    $array_themes[] = array('child' => $val->name, 'parent' => NULL); //MULTIDIMENSIONNAL ARRAY
                    $result = $this->getChild($val, $array_themes); // PASSING $val ENTIRELY TO KEEP PARENT NAME IN FUNCTION
                }//end of foreach child of root
            }//end of existing child of root 
        }
    }//end of foreach root
}//end of existing root 

在getChild函数中,比如:

function getChild($root, $actualParent, $array_themes) {
    //SINCE NOW IT'S $root AND NOT ONLY id, YOU NEED TO UPDATE A LITTLE
    if($this->getCategoryTable()->checkHasRowsChildren($root->id)){
        $rootChild = $this->getCategoryTable()->getRowsChildren($root->id);
        if($rootChild->count() > 0){
            foreach($rootChild as $key => $val) {
                $array_themes[] = array('child' => $val->name, 'parent' => $root->name); // MULTIDIMENSIONNAL ARRAY
                $this->getChild($val, $array_themes); // STILL GIVING ALL $val
            }//end of foreach child of root
        }//end of existing child of root 
    }
}

有了这个,你应该得到一个多维数组接近你想要的。您应该注意,您将得到的顺序与您想要的顺序不同,因此您可能需要进行一些更改,以使其完全按照您想要的方式工作。

希望能有所帮助。

相关内容

  • 没有找到相关文章

最新更新