Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6388231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:15:12+00:00 2026-05-25T03:15:12+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:15:12+00:00Added an answer on May 25, 2026 at 3:15 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let me try to explain my problem. I'm currently trying to develop a small
I'll try to explain my problem the best I can, here goes: I have
It might sound unnecessary, but let me explain my problem first. Probably then it
I'm struggling with add_custom_command. Let me explain the problem in detail. I've these set
Quite new to maven here so let me explain first what I am trying
I'll try to explain the problem in the math language. Assume I have a
I'll try to explain my problem. I use the JSF 1.2 implementation of IBM
I have a little problem with elFinder. I'll try to explain the best I
Let me first try to explain what I'm trying to achieve. I'm making a
Let me try to explain what I have on view and what's I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.