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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:04:39+00:00 2026-05-30T07:04:39+00:00

In another thread i asked about flatten an array with a specific style to

  • 0

In another thread i asked about flatten an array with a specific style to get something like this:

array(4) {
  ["one"]=> string(9) "one_value"
  ["two-four"]=> string(10) "four_value"
  ["two-five"]=> string(10) "five_value"
  ["three-six-seven"]=> string(11) "seven_value"
}

I’ve got some very good help there, but now im wondering how would i reverse this method to get again the same original array:

array(
    'one' => 'one_value',
    'two' => array
        (
            'four' => 'four_value',
            'five' => 'five_value'
        ),

    'three' => array
        (
            'six' => array
                (
                    'seven' => 'seven_value'
                )

        )
)

I’ve tried with recursive method but with no luck.
I thank all the help in advance!

  • 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-30T07:04:40+00:00Added an answer on May 30, 2026 at 7:04 am
    function expand($flat) {
        $result = array();
        foreach($flat as $key => $val) {
            $keyParts = explode("-", $key);
            $currentArray = &$result;
            for($i=0; $i<count($keyParts)-1; $i++) {
                if(!isSet($currentArray[$keyParts[$i]])) {
                    $currentArray[$keyParts[$i]] = array();
                }
                $currentArray = &$currentArray[$keyParts[$i]];
            }
            $currentArray[$keyParts[count($keyParts)-1]] = $val;
        }
        return $result;
    }
    

    Note that the code above is not tested and is given only to illustrate the idea.
    The & operator is used for $currentArray to store not the value but the reference to some node in your tree (implemented by multidimensional array), so that changing $currentArray will change $result as well.

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

Sidebar

Related Questions

So I semi-asked this in another thread about how to get .max and return
[Note: There is another thread about this problem but it did not answer the
I already asked a question about this yesterday, but now I have another :)
Asked this on another thread and thinking I had gotten a solution marked it
I have asked another question related to this in this thread Where to put
Today i was asked this question about sharing data from a thread t1 that
I originally started this question in another thread, but that thread was sorta, kinda
I have one thread that writes results into a Queue. In another thread (GUI),
I asked a non-related question in another thread scons dependency problems that was solved
I know questions like this have been asked before, and I've viewed a lot

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.