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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:19:17+00:00 2026-05-27T03:19:17+00:00

I have an array with n elements in it, with each element containing n

  • 0

I have an array with n elements in it, with each element containing n child elements, each containing…

Array
(
    [tea] => Array
        (
            [drink] => Array
                (
                    [food] => 
                )
        )
    [biscuits] => Array
        (
            [snack] => Array
                (
                    [food] => 
                )
        )
    ...
)

What I want to do is have the inner most element on the outside, and the outer most elements on the inside:

Array
(
    [food] => Array
        (
            [drink] => Array
                (
                    [tea] => 
                )
            [snack] => Array
                )
                    [biscuits] => 
                (
        )
    ...
)

And the solution needs to be able to deal with n children arrays. I am aware of How do I invert a multidimensional array in PHP but the solutions there did not solve this problem.

  • 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-27T03:19:18+00:00Added an answer on May 27, 2026 at 3:19 am

    I’m pretty sure this could be condensed further, but it does the job:

    function flatten(array $array) {
        $key = array(key($array));
        $val = current($array);
        if (is_array($val)) {
            $key = array_merge(flatten($val), $key);
        }
        return $key;
    }
    
    function build(array $path, array $result) {
        $key = array_shift($path);
        if (!isset($result[$key])) {
            $result[$key] = $path ? array() : null;
        }
        if ($path) {
            $result[$key] = build($path, $result[$key]);
        }
        return $result;
    }
    
    $result = array();
    foreach ($array as $key => $value) {
        $result = build(flatten(array($key => $value)), $result);
    }
    

    Demo: http://codepad.org/rnZPdWGG

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

Sidebar

Related Questions

I have an NSMutable array of N elements. Each element is also an NSMutable
I have a 48x365 element numpy array where each element is a list containing
Suppose I have an array that mimics a database table. Each array element represents
I have a two dimensional JSON array where each element contains several attributes. The
I have to load an array of doubles from a file, multiply each element
Consider I have an array of elements out of which I want to create
VISUAL C++ Question Hi, I have array of 3 elements and I want to
I want to apply a function to each element of a cell array --
I have a table with a each td element containing an input tag. I
In PHP I have an array containing 20 elements or more. The keys have

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.