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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:55:19+00:00 2026-05-13T10:55:19+00:00

I am trying to take a flat array and recreate it so that it’s

  • 0

I am trying to take a flat array and recreate it so that it’s multidimensional. I’ve been looking into array_combine and array_merge, but I’m not sure that either of those will give me what I’m hoping for…

The array, in it’s current form (and this is just a simplified example):

Array
(
    [0] => stdClass Object
        (
            [tid] => 31
            [name] => Safeway
            [parents] => Array
                (
                    [0] => 0
                )

        )

    [1] => stdClass Object
        (
            [tid] => 32
            [name] => Dairy
            [parents] => Array
                (
                    [0] => 31
                )

        )

    [2] => stdClass Object
        (
            [tid] => 33
            [name] => Milk
            [parents] => Array
                (
                    [0] => 32
                )

        )
)

I’m trying to create a multidimensional array where each object is a subarray of it’s parent. So, in the example above, I’m trying to output:

Array
(
    [0] => stdClass Object
        (
            [tid] => 31
            [name] => Safeway
            [children] => Array
                (
                    [tid] => 32
                    [name] => Dairy
                    [children] => Array
                        (
                            [tid] => 33
                            [name] => Milk
                        )
                )

        )
)
  • 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-13T10:55:20+00:00Added an answer on May 13, 2026 at 10:55 am

    First off, what you are showing is not an multidimensional array, but an array of StdClass objects.

    If it’s alright with you to make them truely arrays then this might do it:

    // initiate result array
    $multiArray = array();
    
    // assume $items is your current array
    foreach( $items as $item )
    {
        // cast StdClass to array
        $objToArray = (array) $item;
    
        // if this item is initiated already merge it with the current item
        $multiArray[ $objToArray[ 'tid' ] ] = isset( $multiArray[ $objToArray[ 'tid' ] ] ) ? $multiArray[ $objToArray[ 'tid' ] ] + $objToArray : $objToArray;
    
        foreach( $objToArray[ 'parents' ] as $parentId )
        {
            // if parents don't exist yet, initiate them
            if( !isset( $multiArray[ $parentId ] ) )
            {
                $multiArray[ $parentId ] = array(
                    'children' => array()
                );
            }
    
            // add this item to the parents children collection by reference (for efficiency)
            $multiArray[ $parentId ][ 'children' ][ $objToArray[ 'tid' ] ] = &$multiArray[ $objToArray[ 'tid' ] ];
        }
    }
    

    With this you can easily find items by id with:

    $item = $multiArray[ $someId ];
    

    And to get a child:

    $child = $item[ 'children' ][ $someChildId ];
    

    Or all children:

    $children = $item[ 'children' ];
    

    EDIT
    Ok, I’ve tested it now, and it seems to work fine after adding some missing semicolons.

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

Sidebar

Related Questions

I am trying to take an array that contains 400 integers, and split it
Trying to take an Array position(toString) and pass that to a new activity, then
Im trying to find a php/js script that will let me take an image,
I'm trying take the names that I am given by the program after it
I'm trying to take an array of ID numbers and update every row with
I am trying to take a string like php,mysql,css and turn it into #php
Trying to take a string of tags and only save the first 10 that
Ok I am trying to take a string and put it into the intent
trying to take a line of text and tokenize it into a filename and
I'm trying to take a string, and convert it into a variable name. I

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.