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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:46:27+00:00 2026-06-14T22:46:27+00:00

I have tree of objects: array(4) ( 0 => object stdClass(14) { public id

  • 0

I have tree of objects:

array(4) (
    0 => object stdClass(14) {
        public id => string(1) "1"
        public parent_id => string(1) "0"
        public name => string(18) "Stationary engines"
        public uri => string(18) "stationary-engines"
        public created => string(19) "2012-11-19 15:15:34"
        public updated => NULL
        public subcategories => array(4) (
            0 => object stdClass(14) {
                public id => string(1) "5"
                public parent_id => string(1) "1"
                public name => string(6) "Yanmar"
                public uri => string(6) "yanmar"
                public created => string(19) "2012-11-19 15:23:36"
                public updated => NULL
                public subcategories => array(1) (
                    0 => object stdClass(14) {
                        public id => string(2) "15"
                        public parent_id => string(1) "5"
                        public name => string(18) "Yanmar subcategory"
                        public uri => string(18) "yanmar-subcategory"

                        public created => string(19) "2012-11-21 16:38:06"
                        public updated => NULL
                        public subcategories => array(1) (
                            0 => object stdClass(13) {
                                public id => string(2) "16"
                                public parent_id => string(2) "15"
                                public name => string(30) "Yanmar subcategory subcategory"
                                public uri => string(30) "yanmar-subcategory-subcategory"
                                public created => string(19) "2012-11-21 17:37:00"
                                public updated => NULL
                            }
                        )
                    }
                )
            }
            1 => object stdClass(13) {
                public id => string(1) "6"
                public parent_id => string(1) "1"
                public name => string(11) "Мercruiser"
                public uri => string(10) "mercruiser"
                public created => string(19) "2012-11-19 15:23:36"
                public updated => NULL
            }
            2 => object stdClass(13) {
                public id => string(1) "7"
                public parent_id => string(1) "1"
                public name => string(11) "Volvo-Penta"
                public uri => string(11) "volvo-penta"
                public created => string(19) "2012-11-19 15:24:49"
                public updated => NULL
            }
            3 => object stdClass(13) {
                public id => string(1) "8"
                public parent_id => string(1) "1"
                public name => string(27) "Basic configuration engines"
                public uri => string(27) "basic-configuration-engines"
                public created => string(19) "2012-11-19 15:24:49"
                public updated => NULL
            }
        )
    }
    1 => object stdClass(14) {
        public id => string(1) "2"
        public parent_id => string(1) "0"
        public name => string(10) "Generators"
        public uri => string(10) "generators"
        public created => string(19) "2012-11-19 15:15:58"
        public updated => NULL
        public subcategories => array(4) (
            0 => object stdClass(13) {
                public id => string(1) "9"
                public parent_id => string(1) "2"
                public name => string(6) "Diesel"
                public uri => string(6) "diesel"
                public created => string(19) "2012-11-19 15:34:38"
                public updated => NULL
            }
            1 => object stdClass(13) {
                public id => string(2) "10"
                public parent_id => string(1) "2"
                public name => string(8) "Gasoline"
                public uri => string(8) "gasoline"
                public created => string(19) "2012-11-19 15:34:38"
                public updated => NULL
            }
            2 => object stdClass(13) {
                public id => string(2) "11"
                public parent_id => string(1) "2"
                public name => string(6) "Kohler"
                public uri => string(6) "kohler"
                public created => string(19) "2012-11-19 15:35:35"
                public updated => NULL
            }
            3 => object stdClass(13) {
                public id => string(2) "12"
                public parent_id => string(1) "2"
                public name => string(13) "Fischer Panda"
                public uri => string(13) "fischer-panda"
                public created => string(19) "2012-11-19 15:36:14"
                public updated => NULL
            }
        )
    }
    2 => object stdClass(14) {
        public id => string(1) "3"
        public parent_id => string(1) "0"
        public name => string(14) "Boat equipment"
        public uri => string(14) "boat-equipment"
        public created => string(19) "2012-11-19 15:16:59"
        public updated => NULL
        public subcategories => array(1) (
            0 => object stdClass(13) {
                public id => string(2) "13"
                public parent_id => string(1) "3"
                public name => string(11) "Subcategory"
                public uri => string(11) "subcategory"
                public created => string(19) "2012-11-19 15:37:12"
                public updated => NULL
            }
        )
    }
    3 => object stdClass(14) {
        public id => string(1) "4"
        public parent_id => string(1) "0"
        public name => string(11) "Spare parts"
        public uri => string(11) "spare-parts"
        public created => string(19) "2012-11-19 15:16:59"
        public updated => NULL
        public subcategories => array(1) (
            0 => object stdClass(13) {
                public id => string(2) "14"
                public parent_id => string(1) "4"
                public name => string(11) "Consumables"
                public uri => string(11) "consumables"
                public created => string(19) "2012-11-19 15:37:39"
                public updated => NULL
            }
        )
    }
)

And I need to convert it to associative array like this:

array(
    '1' => 'category name',
    '3' => '   subcategory name',
    '4' => '      subcategory name',
    '2' => 'category2 name',
    '5' => '   subcategory2 name',
);

Each subcategory should be indented by spaces.
But I’m not really familiar with recursion and need some help, I would really appreciate if you could help me.
Thanks in advance.

UPDATE:
I’ve ended up with following:

$options = array();
$this->to_array($tree, $options, '   ', 0);

function to_array($tree, & $target, $indention = '', $lvl = 0)
{
    foreach($tree as $leaf)
    {
        $target[$leaf->id] = str_repeat($indention, $lvl).$leaf->name;

        if (isset($leaf->subcategories))
        {
            $this->to_array($leaf->subcategories, $target, $indention, $lvl + 1);
        }
    }
}

any suggestions on improvement?

  • 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-06-14T22:46:29+00:00Added an answer on June 14, 2026 at 10:46 pm

    I would go for something along the lines of:

    function printArray($arr, &$target, $header = '') {
      foreach($arr as $category) {
        $target[$category->id] = $header . $category->name;
        printArray($category->subcategories, $target, $header . ' ');
      }
    }
    
    // And then, from your source
    $targetArray = array();
    printArray($source, $targetArray);
    

    That doesn’t deal with possible collisions, but if your data is coming from a single table, then collisions should not happen.

    To customize even more, you pass the header modification as a parameter to the recursive function like:

    function printArray($arr, &$target, $header = '', $headerIncrement = ' ') {
      foreach($arr as $category) {
        $target[$category->id] = $header . $category->name;
        printArray($category->subcategories, $target, $header . $headerIncrement, $headerIncrement);
      }
    }
    

    That would allow you to call it with

    printArray($source, $target, '', '   ');
    //or even
    printArray($source, $target, '', '<span class="spacer">&nbsp;</span>');
    

    And add styling in your css to change the spacing how you want.

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

Sidebar

Related Questions

I have the following xml: <objects> <property name=steps> <property name=array> <object type=com.tn.assistant.models.Step> <property name=preDialogues>
In Lua, I have a tree relationship structure between objects where an object can
I have a business object project, which contains composite structure: public class Tree {
I have a tree-like object structure that consists of two types of objects: object
I have a custom object that contains an array (called children) where objects of
I have a mx:Tree with an object as datasource. An array with this structure
I have a tree that consists of several objects, where each object has a
I have a large array with around 20k objects in it. Each object has
I have this ArrayList - ArrayList<Object[]> tree ... I also have this Array -
I have a tree of objects in my viewModel. The objects are something like

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.