值不会推送递归数组

时间:2011-09-02 09:18:11

标签: php recursion

让我解释一下我的问题。我尝试生成一系列类别。

这是我的功能。

private function recursiveCategoriesTree($id_parent, $level, $categories)
{
    $tree = array();

    foreach($categories as $categorie)
    {   
        if($id_parent == $categorie['id_parent'])
        {           
            $tree[$level][] = $categorie;
            $this->recursiveCategoriesTree($categorie['id_category'], ($level+1), $categories);
        }           
    }

    return $tree;       

}

当我使用echo跟踪循环时,一切似乎都有效,所有父类别和女孩都被覆盖,但不会被推入数组。

以下是我的类别print_r

array( 
[0] => Array
    (
        [id_category] => 4
        [name] => Pièces détachées
        [id_parent] => 1
    )

[1] => Array
    (
        [id_category] => 5
        [name] => Excavateur
        [id_parent] => 4
    )

[2] => Array
    (
        [id_category] => 6
        [name] => série 100
        [id_parent] => 5
    )

[3] => Array
    (
        [id_category] => 7
        [name] => above
        [id_parent] => 6
    )

[4] => Array
    (
        [id_category] => 8
        [name] => système hydraulique
        [id_parent] => 7
    )

[5] => Array
    (
        [id_category] => 9
        [name] => série 200
        [id_parent] => 5
    )

[6] => Array
    (
        [id_category] => 10
        [name] => thru
        [id_parent] => 6
    )

[7] => Array
    (
        [id_category] => 11
        [name] => Compaction
        [id_parent] => 4
    )
)

以下是生成print_r

的结果
Array(
[0] => Array
    (
        [0] => Array
            (
                [id_category] => 5
                [name] => Excavateur
                [id_parent] => 4
            )

        [1] => Array
            (
                [id_category] => 11
                [name] => Compaction
                [id_parent] => 4
            )

    )
)

我将我的功能称为

$tree = $this->recursiveCategoriesTree(4, 0, $categories)

有什么问题?谢谢你=)

3 个答案:

答案 0 :(得分:2)

recursiveCategoriesTree()返回$tree,但是当您以递归方式调用该方法时,您没有对该返回值执行任何操作。您只存储从方法初始调用返回的$tree

也许你想要这样的东西?

$categorie['children'] = $this->recursiveCategoriesTree($categorie['id_category'], ($level+1), $categories);

答案 1 :(得分:2)

从递归调用中获取返回值并将其推送到数组,或者创建名为tree的类的私有属性,然后将值推送到该数组。您没有在递归函数调用中传递变量$tree

E.g。如果你这样做,它将起作用:(编辑:固定...... [再次])

private $catTree = array();
private $catStartLevel = FALSE;

private function recursiveCategoriesTree($id_parent, $level, $categories) {

    if ($this->catStartLevel !== FALSE) $this->catStartLevel = $level;

    foreach($categories as $categorie) {    

      if($id_parent == $categorie['id_parent']) {           

        $this->catTree[$level][] = $categorie;
        $this->recursiveCategoriesTree($categorie['id_category'], ($level+1), $categories);

      }         

    }

    if ($this->catStartLevel === $level) {

      $tree = $this->catTree;
      $this->catTree = array();
      $this->catStartLevel = FALSE;

    }

    return $tree;       

}

...但是这并不好,因为你现在在班上有一个“毫无意义”的私人财产。最好更改数组结构,并从$this->recursiveCategoriesTree() ...

中捕获返回值

修改

考虑一下,如果你真的想要那个结构中的数组,你可能最好传递变量来填充数组by reference

private function recursiveCategoriesTree($id_parent, $level, $categories, &$tree) {

    foreach($categories as $categorie) {    

      if($id_parent == $categorie['id_parent']) {           

        $tree[$level][] = $categorie;
        $this->recursiveCategoriesTree($categorie['id_category'], ($level+1), $categories, $tree);

      }         

    }

}

...然后你会这样称呼它......

$myTree = array();
$obj->recursiveCategoriesTree($id_parent, $level, $categories, $myTree);
print_r($myTree);

答案 2 :(得分:0)

在将类别附加到树之前,您应首先获取类别的所有子项,并将它们添加到其数组中。有点像这样:

foreach($categories as $categorie)
{   
    $categorie['childs'] = $this->recursiveCategoriesTree($categorie['id_category'], ($level+1), $categories);
    $tree[$level][] = $categorie;
}