开发者

values not push on recursive array

开发者 https://www.devze.com 2023-04-02 12:37 出处:网络
Let me explain my problem. I try to generate an array of categories. Here is my function. private function recursiveCategoriesTree($id_parent, $level, $categories)

Let me explain my problem. I try to generate an array of categories.

Here is my function.

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;       

}

When I trace the loop with echo, everything seems to work, all the parent categories and girls are covered, but are not pushed into the array.

Here is the print_r of my categories

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_paren开发者_如何学Ct] => 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
    )
)

Here is the result of print_r generated

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

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

    )
)

I call my function like that

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

What is the problem ? thank you =)


Either get the return value from your recursive call and push that onto the array, or make a private property of the class called tree and push values onto that instead. You are not passing the variable $tree across recursive function calls.

E.g. if you do this it will work: (EDIT: Fixed... [again])

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;       

}

...however this is not great, because you now have a 'pointless' private property in your class. You would be better to change you array structure, and catch the return values from $this->recursiveCategoriesTree()...

EDIT

Thinking about it, if you really want the array in that structure, you would probably be better to pass the variable to be populated with the array 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);

      }         

    }

}

...and then you would call it like this...

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


recursiveCategoriesTree() returns the $tree, but you're not doing anything with that return value when you're calling the method recursively. You're only storing the $tree returned from the initial call to the method.

Perhaps you want something like this?

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


You should first fetch all the childs of a category, and add them to its array, before appending the category to the tree. Kind of like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消