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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:56:08+00:00 2026-06-16T17:56:08+00:00

Suppose I have a one-dimensional array $arr like this: Array ( [‘key1’] => 1

  • 0

Suppose I have a one-dimensional array $arr like this:

Array
(
    ['key1'] => 1
    ['key2'] => 1
    ['key3'] => 1
    ['key4'] => 1
)

I want to loop through $arr like this:

foreach($arr as $key => $a) {
   //$tree[????] = ????? Here is my concern
}

such that, doing print_r($tree) produces

Array
(
    ['key1'] => Array
    (
        ['key2'] => Array
        (
            ['key3'] => Array
            (
                ['key4'] => 1
            )
        )
    )
)

My concern is how to increase the dimension (not the values) of the array $tree inside the loop from a single dimension array $numbers such that $tree['key1']['key2']['key3'] is an array and $tree['key1']['key2']['key3']['key4'] is equal to 1.

Furthermore, if I have a single dimensional array $foo with 10 elements. I should produce another array $bar that expands to 10 dimensional array similar to the output above.

What should I do inside the foreach loop? Or instead of using loop, is there a way to produce an output like that of above from a one dimension array?


EDIT:

OK well you obviously need some kind of recursive function, but can
you explain how you know that key2 should be a child of key1, key3
should be a child of key2 etc based on the data you show?

The next element of a 1D array is a child of the previous element. So if the 1D array is like this:

Array
(
    ['bar'] => 1 // I don't care of the values as of the moment
    ['foo'] => 1
    ['baz'] => 1
)

Element foo should be the child of bar, and element baz should be the child of foo for the tree array.

Ok so the actual values of everything except the last element are
irrelevant?

Actually all values of the 1D array is irrelevant as of this moment. I am only concerned on constructing the tree array.

  • 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-16T17:56:10+00:00Added an answer on June 16, 2026 at 5:56 pm

    The way to do this is to use references.

    function create_tree($arr) {
      $result = array();
      $ref = &$result;
      foreach ($arr as $key => $el) {
        $ref = array($key => $el);
        $ref =& $ref[$key];
      }
      return $result;
    }
    

    How it works:

    $result = array(); // an array to hold the result
    
    $ref = &$result; // start with a reference to the top level
    
    foreach ($arr as $key => $el) { // iterate over the input array
    
      $ref = array($key => $el); // create this level in the array
    
      $ref =& $ref[$key]; // change the reference to be the new deepest level
    
    }
    
    return $result; // return the result
    

    See it working

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

Sidebar

Related Questions

Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
Suppose I have a string like this: one two three four five six seven
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
Suppose I have one button, and I want it to fetch data (like an
Suppose i have one table that holds Blogs. The schema looks like : ID
Suppose i have one website with simple pages in php like page1.php page2.php Now
Suppose we have a simple query like this: SELECT x FROM t WHERE t.y
Suppose I have a multi-dimensional array of the form: array ( array('Set_ID' => 1,
suppose I have an object like this: class Spline { public: Spline(std::size_t const dim);
I have a numpy one dimensional array c that is supposed to be filled

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.