【问题标题】:Get array child from recursive function从递归函数获取数组子
【发布时间】:2014-08-21 17:15:10
【问题描述】:

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

$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
乙:甲
答:E
E : D
D:空

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

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

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

【问题讨论】:

  • 请显示源(原始)数组的外观
  • $array_result = array( [0]=>"H" );

标签: php arrays function recursion multidimensional-array


【解决方案1】:

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

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

希望对你有帮助。

【讨论】:

  • 我只得到了父子级别 1 而不是另一个级别。是因为函数getChild上的$this->getChild没有调用?
  • 也许只是调用function getChild($root, $actualParent, &$array_themes)(通过引用传递)并在你的foreach根之后输出$array_themes
  • 感谢您的帮助。我在变量前面添加 & 成为 &$array_themes 并且它现在可以工作了。
  • 没问题,请记住,您可以在课堂上使用var $array_themes = array();,然后使用$this->array_themes 而不是$array_themes 调用它。可能会更好,这取决于您的使用情况。
猜你喜欢
  • 2012-08-13
  • 2021-09-18
  • 2016-08-17
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 2019-02-23
  • 2021-08-04
  • 1970-01-01
相关资源
最近更新 更多