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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:15:14+00:00 2026-06-10T00:15:14+00:00

I have an array that looks like this array( 1 => array( ‘id’ =>

  • 0

I have an array that looks like this

array(
    1 => array(
        'id'     => 1,
        'name'   => 'first',
        'parent' => null
    ),
    2 => array(
        'id'     => 2,
        'name'   => 'second',
        'parent' => null
    ),
    3 => array(
        'id'     => 3,
        'name'   => 'third',
        'parent' => 1
    ),
    4 => array(
        'id'     => 4,
        'name'   => 'fourth',
        'parent' => 3
    ),
    5 => array(
        'id'     => 5,
        'name'   => 'fifth',
        'parent' => 1
    ),        
);

But I want to move any “child” items to the “children” key under the array.
So, I want to end up with

array(
    1 => array(
        'id'       => 1,
        'name'     => 'first',
        'parent'   => null,
        'children' => array(
            3 => array(
                'id'       => 3,
                'name'     => 'third',
                'parent'   => 1,
                'children' => array(
                    4 => array(
                        'id'       => 4,
                        'name'     => 'fourth',
                        'parent'   => 3,
                        'children' => array()
                    ),
                )
            ),
            5 => array(
                'id'       => 5,
                'name'     => 'fifth',
                'parent'   => 1,
                'children' => array()
            )
        )
    ),
    2 => array(
        'id'       => 2,
        'name'     => 'second',
        'parent'   => null,
        'children' => array()
    )
);

But to be honest I have absolutely no idea where to start.

I was thinking maybe looping over each item in the array and then building the new array as I go with $new_array[$current['parent']]['children'][$current['id']] = $current;

But I run into issues as soon as I hit a nested item.

I could build a function that takes the current array, and the entire array and recursively moves up the tree to find all the parents and therefore the entire path, but I run into issues again if one of the parents has not been created yet.

The only option I could think of is to build an array map of the different levels of parents and the loop over that recursively and grab all the elements that way, but that seems somewhat inefficient?

Can anyone suggest a solution?

  • 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-10T00:15:16+00:00Added an answer on June 10, 2026 at 12:15 am

    You had the right idea with the foreach loop. What you want to do, however, requires the ‘magic’ of references.

    foreach($oldArray as $key => &$item) {
        if($item["parent"] == null) $newArray[$key] = &$item;
        else $oldArray[$item["parent"]]["children"][$key] = &$item;
    }
    
    unset($item);
    

    This will output for ‘$oldArray’

    Array
    (
    [1] => Array
        (
            [id] => 1
            [name] => first
            [parent] => 
            [children] => Array
                (
                    [3] => Array
                        (
                            [id] => 3
                            [name] => third
                            [parent] => 1
                            [children] => Array
                                (
                                    [4] => Array
                                        (
                                            [id] => 4
                                            [name] => fourth
                                            [parent] => 3
                                        )
    
                                )
    
                        )
    
                    [5] => Array
                        (
                            [id] => 5
                            [name] => fifth
                            [parent] => 1
                        )
    
                )
    
        )
    
    [2] => Array
        (
            [id] => 2
            [name] => second
            [parent] => 
        )
    
    [3] => Array
        (
            [id] => 3
            [name] => third
            [parent] => 1
            [children] => Array
                (
                    [4] => Array
                        (
                            [id] => 4
                            [name] => fourth
                            [parent] => 3
                        )
    
                )
    
        )
    
    [4] => Array
        (
            [id] => 4
            [name] => fourth
            [parent] => 3
        )
    
    [5] => Array
        (
            [id] => 5
            [name] => fifth
            [parent] => 1
        )
    
    )
    

    And for newArray (the ‘purified’ version)

    Array
    (
    [1] => Array
        (
            [id] => 1
            [name] => first
            [parent] => 
            [children] => Array
                (
                    [3] => Array
                        (
                            [id] => 3
                            [name] => third
                            [parent] => 1
                            [children] => Array
                                (
                                    [4] => Array
                                        (
                                            [id] => 4
                                            [name] => fourth
                                            [parent] => 3
                                        )
    
                                )
    
                        )
    
                    [5] => Array
                        (
                            [id] => 5
                            [name] => fifth
                            [parent] => 1
                        )
    
                )
    
        )
    
    [2] => Array
        (
            [id] => 2
            [name] => second
            [parent] => 
        )
    
    )
    

    Now, why this works:
    By putting &item into the foreach loop, we work with a reference to the item, instead of a copy. That means, whatever we change about that item, we also change at the corresponding array element.

    By passing &$item to $newArray[$key], or to the children array, we pass the reference along…so whatever we do to the ‘original object’ (i.e. [3]), we also do to all the references.

    The unset($item) is necessary, because it erases the binding between the last instance of $item and the variable. Otherwise, we’d change the last variable as well, as soon as we change the $item variable once more. Not exactly necessary in this script, but still a good practice to remember.

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

Sidebar

Related Questions

If I have an array that looks like this: $str = ''; if( $_POST['first']
I have an array that looks like this: [867324] [id] => 867324 [name] =>
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have a array that looks like this Array ( [provider] => Array (
I have an array that looks like this: Array ( [0] => Array (
I have an array that looks like this, Array ( [0] => 849710414 [1]
I have an Array that looks like this: array( 0 => array( 'key1' =>
I have an array that looks like this (sample): Array ( [1600] => Array
I have an array that looks like this Array ( [0] => Array (
Hi I have an array that looks like this : Array ( [0] =>

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.