2

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_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
    )
)

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 =)

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

It works. Indeed, by making the variable global to the class tree ... it works =) Thanks a lot !
2

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);

1 Comment

I need to generate something like that : array[level][n] The level depends only on the parent. Also, I tried your method, and the children Array is empty
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.