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

时间:2014-07-01 08:03:54

标签: php arrays function recursion multidimensional-array

我有来自数据库的子变量。例如:

$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 
    }
}

结果应该是这样的:

孩子:父母

H:G
G:D
J:C
F:C
C:A
B:A
答:E
E:D
D:空

我得到了所有父母和孩子。但最终结果我想得到像

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

可以获得那样的数组结果吗?

1 个答案:

答案 0 :(得分:0)

我认为你非常接近解决方案,你只需要添加谁是当前孩子的父亲,(嵌套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 
    }
}

有了这个,你应该得到一个你想要的多维数组。您应该注意,您获得的订单与您想要的订单不同,因此您可能需要进行一些更改才能完全符合您的要求。

希望它有所帮助。