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 1012415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:56:35+00:00 2026-05-16T09:56:35+00:00

I have the following code (I know that this code is not optimized but

  • 0

I have the following code (I know that this code is not optimized but it’s not for discussion):

function select_categories($cat_id)
{
    $this->db = ORM::factory('category')
            ->where('parent', '=', $cat_id)
            ->find_all();

    foreach ($this->db as $num => $category)
    {
        if($category->parent == 0)
        {
            $this->tmp[$category->parent][$category->id] = array();
        }
        else {
            $this->tmp[$category->parent][$category->id] = array();
        }

        $this->select_categories($category->id);
    }

    return $this->tmp;
}

Function returns this array:

array(3) (
    0 => array(2) (
        1 => array(0) 
        2 => array(0) 
    )
    2 => array(1) (
        3 => array(0) 
    )
    3 => array(2) (
        4 => array(0) 
        5 => array(0) 
    )
)

But how should I change the code

else {
    $this->tmp[$category->parent][$category->id] = array();
        // ^^^^^^^^^^^^^^^^^^^^^^ (this bit)
}

To merge array[3] to array[2][3] for example (because array[3] is a subdirectory of array[2] and array[2] is a subdirectory of array[0][2]), so, I need to make this (when I don’t know the level of subdirectories):

array (
    0 => array (
        1 => array 
        2 => array (
                    3 => array (
                                4 => array
                                5 => array
                                ) 
                    ) 
    ) 
)
  • 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-16T09:56:35+00:00Added an answer on May 16, 2026 at 9:56 am

    Assuming you dont want any data/children tags in your array:

    foreach ($this->db as $num => $category)
    {
        // save the data to the array
        $this->tmp[$category->id] = array();
    
        // save a reference to this item in the parent array
        $this->tmp[$category->parent][$category->id] = &$this->tmp[$category->id];
    
        $this->select_categories($category->id);
    }
    
    // the tree is at index $cat_id
    return $this->tmp[$cat_id];
    

    If you just need to retrieve the full tree out of the database, you can even simplify your query (get all records at once) and remove the recursive call in this function. You will need an extra check that will only set the $this->tmp[$catagory->id] when it does not exist and else it should merge the data with the existing data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code (Yes I know it's quite long winded, but I
I know that this question have been asked several times. But I can't get
I have following code, but validation for the custom type( UserDetails ) are not
Say that I have the following code: function testA { setTimeout('testB()', 1000); doLong(); }
I have the following code: var Company = function(app) { this.crypto = require('ezcrypto').Crypto; var
I want to know is below code correct ? I have following code which
I have to following code and as far as I know it is correct,
I have following code that I am compiling in a .NET 4.0 project namespace
I have the following Code which I know doesn't work correctly. Yes I know
Hallo, I have following jquery code for calling ASP.NET MVC controller method that is

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.