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

Php 从递归函数中获取数组子级,php,arrays,function,recursion,multidimensional-array,Php,Arrays,Function,Recursion,Multidimensional Array,我有来自数据库的带有child的变量。例如: $roots = $this->getCategoryTable()->getCategoryRoot($project_id);//themes if($roots->count()> 0){ foreach($roots as $root){ $root_id = $root->id;

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

$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
A:E
E:D
D:空

我得到了所有的父母和孩子。但在最终结果中,我希望得到如下结果数组

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

有可能得到这样的数组结果吗?

我认为您非常接近解决方案,您只需要添加谁是当前子级的父级,以及(嵌套foreach中的更改):

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


希望有帮助。

请显示源(原始)数组的外观$array_result=array([0]=>“H”);我只得到父级和子级1,而没有得到另一级。这是因为函数getChild上的$this->getChild没有调用?可能只是调用
函数getChild($root、$actualParent、&$array_themes)
(通过引用传递)并在foreach root之后输出
$array_themes
。谢谢您的帮助。我在变量前面添加了&be&$array\u themes,它现在可以工作了。没问题,记住你可以使用
var$array\u themes=array()$this->array\u themes
而不是
$array\u themes
调用它。可能更好,取决于您的使用。
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 
    }
}