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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:36:11+00:00 2026-05-18T09:36:11+00:00

I’m using PHP and I need help with a seemingly simple task with an

  • 0

I’m using PHP and I need help with a seemingly simple task with an array.

This is my example array:

$arr = array(
    0  => NULL,
    1  => NULL,
    2  => NULL,
    3  => NULL,
    8  => '2',
    9  => '2',
    10 => '2',
    11 => '2',
    12 => '3',
    13 => '3',
    14 => '8',
    15 => '8',
    16 => '14',
    17 => '14',
    18 => '14'
);

The keys of the array represent the IDs (unique).
The values are the parentIDs, i.e. the ID of the parent “node”. NULL means that there is no parentID (i.e. 1st dimension of the new array).

Now, I need to create a new, multi-dimensional array that has all the child elements under their parent IDs. (This probably sounds very confusing, sorry for my lack of descriptive abilities. There’s an example below, which should make things clearer)

Here’s what the new array of my example would look like after the “sorting” function, or whatever you call this, was applied:

$arr = array(
 0 => array(),
 1 => array(),
 2 => array(
     8 => array(
        14 => array(
            16 => array(),
            17 => array(),
            18 => array()
),
        15 => array()
),
     9 => array(),
    10 => array(),
    11 => array()
),
 3 => array(
    12 => array(),
    13 => array()
)
);

I know all the empty array()s are probably not a very clean and elegant solution but unfortunately this is the way I need it to be!

  • 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-18T09:36:12+00:00Added an answer on May 18, 2026 at 9:36 am

    This recursive function will add the given datum to the correct parent, and should be called once for each element in your starting array.

    function add_branch(&$tree, $datum, $parent) {
    
        // First we have the base cases:
        // If the parent is NULL then we don't need to look for the parent
        if ($parent == NULL) {
            $tree[$datum] = array();
            return true;
        }
    
        // If the array we've been given is empty, we return false, no parent found in this branch
        if (! count($tree)) {
            return false;
        }
    
    
        // We loop through each element at this level of the tree...
        foreach($tree as $key => $val) {
    
            // If we find the parent datum...
            if ($key == $parent) {
    
                // We add the new array in and we're done.
                $tree[$key][$datum] = array();
                return true;
            }
    
            // Otherwise, check all the child arrays
            else {
    
                // Now we check to see if the parent can be found in the curent branch
                // If a recursive call found a parent, we're done
                if (add_branch($tree[$key], $datum, $parent)) {
                    return true;
                }
            }
        }
    
        // If none of the recursive calls found the parent, there's no match in this branch
        return false;
    
    }
    

    The comments are quite verbose, in the hope that you can understand what is going on. I’d encourage you to do a bit of reading on recursive functions to get your head around it.

    This is how it would be used:

    $arr = array(
     0 => NULL,
     1 => NULL,
     2 => NULL,
     3 => NULL,
     8 =>  '2',
     9 =>  '2',
    10 =>  '2',
    11 =>  '2',
    12 =>  '3',
    13 =>  '3',
    14 =>  '8',
    15 =>  '8',
    16 => '14',
    17 => '14',
    18 => '14'
    );
    
    
    $final = array();
    
    foreach ($arr as $datum => $parent) {
        add_branch($final, $datum, $parent);
    }
    

    $final now has the correct finishing array, as indicated in the question.

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

Sidebar

Related Questions

No related questions found

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.