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

  • Home
  • SEARCH
  • 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 6696397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:20:12+00:00 2026-05-26T06:20:12+00:00

I’ve got a one-dimensional array of objects that represent multi-dimensional data: array( array( id

  • 0

I’ve got a one-dimensional array of objects that represent multi-dimensional data:

array(
    array(
        "id" => 45,
        "parent_id" => null
    ),
    array(
        "id" => 200,
        "parent_id" => 45
    ),
    array(
        "id" => 345,
        "parent_id" => 45
    ),
    array(
        "id" => "355",
        "parent_id" => 200
    )
);

How should I convert it into a multi-dimensional array:

array(
    array(
        "id" => 45,
        "parent_id" => null,
        "children" => array(
            array(
                "id" => 200,
                "parent_id" => 45,
                "children" => array(
                    "id" => "355",
                    "parent_id" => 200
                )

            ),
            array(
                "id" => 345,
                "parent_id" => 45
            ),
        )
    ),
);
  • 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-26T06:20:13+00:00Added an answer on May 26, 2026 at 6:20 am

    The following code-example converts the array $array into the tree-structure you’re looking for:

    // key the array by id
    $keyed = array();
    foreach($array as &$value)
    {
        $keyed[$value['id']] = &$value;
    }
    unset($value);
    $array = $keyed;
    unset($keyed);
    
    // tree it
    $tree = array();
    foreach($array as &$value)
    {
        if ($parent = $value['parent_id'])
            $array[$parent]['children'][] = &$value;
        else
            $tree[] = &$value;
    }
    unset($value);
    $array = $tree;
    unset($tree);
    
    var_dump($array); # your result
    

    This does not work, if there is an existing parent id that is 0. But could be easily changed to reflect that.

    This is a related question, that has the original array already keyed, so the first half of the solution could be spared: Nested array. Third level is disappearing.

    Edit:

    So how does this work? This is making use of PHP variable aliasing (also known as references) and (temporary) arrays that are used to store a) aliases to the nodes ($keyed) and b) to build the new tree order ($tree).

    Could you […] explain the purpose of $array = $keyed, $array = $tree and the unsets?

    As both, $keyed and $tree contain references to values in $array, I first copy over that information into $array, e.g.:

    $array = $keyed;
    

    As now $keyed is still set (and contains references to the same values as in $array), $keyed is unset:

    unset($keyed);
    

    This un-sets all references in $keyed and ensures, that all values in $array aren’t referenced any longer (the value’s refcount is reduced by one).

    If the temporary arrays are not unset after the iteration, their references would still exist. If you use var_dump on $array, you would see that all values would have a & in front, because they are still referenced. unset($keyed) removes these references, var_dump($array) again, and you will see the &s are gone.

    I hope this was understandable, references can be hard to follow sometimes if you’re not fluent with them. It often helps me to think about them as variable aliases.

    If you want some exercise, consider the following:

    How to convert your $array from flat to tree with one foreach iteration?

    Decide on your own when you would like to click the link which contains a Solution.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
I have a French site that I want to parse, but am running into
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from
I want to count how many characters a certain string has in PHP, but

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.