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

  • Home
  • SEARCH
  • 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 5943291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:22:28+00:00 2026-05-22T16:22:28+00:00

I have a standard array with key-value pairs – and I want to use

  • 0

I have a standard array with key-value pairs – and I want to use the keys to transform it into a multi-dimensional array. The difficulty seems to be that I need to loop recursively the unknown number of new keys and turn them into a multi-dimensional array. In short, I want this:

$val[alfa.xray.uno] = "Some value";    
=> $val['alfa']['xray']['uno'] = "Some value";    

Example:
(The code fails and also needs to handle N dimensions – but you get the idea..)

$arr['alfa.xray.uno'] = "Alfa X-Ray Uno";
$arr['alfa.yaho.duo'] = "Alfa Yaho Duo";
$arr['beta.xray.uno'] = "Beta X-Ray Uno";
$arr['beta.xray.duo'] = "Beta X-Ray Duo";
$arr['just-me'] = "Root-level item";

print_r( array_flat_to_multidimensional($arr) );

function array_flat_to_multidimensional($arr) {
    foreach($arr as $key=>$val) {
        $key = explode(".",$key);
        for($i=0; $i<count($key); $i++) {
            if($i==0) { $out[$key[$i]] = $val; }
            else if($i==1) { $out[$key[$i-1]][$key[$i]] = $val; }
            else if($i==2) { $out[$key[$i-2]][$key[$i-1]][$key[$i]] = $val; }
            else if($i==3) { $out[$key[$i-3]][$key[$i-2]][$key[$i-1]][$key[$i]] = $val; }
        }
    }
    return $out;
}

Perhaps RecursiveArrayIterator will do the trick?

  • 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-22T16:22:28+00:00Added an answer on May 22, 2026 at 4:22 pm

    You could use references to successively iterate through and build up the nested array structure:

    $out = array();
    foreach ($arr as $key=>$val) {
        $r = & $out;
        foreach (explode(".", $key) as $key) {
            if (!isset($r[$key])) {
                $r[$key] = array();
            }
            $r = & $r[$key];
        }
        $r = $val;
    }
    return $out;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to dynamically access value of variable, let's say I have this array:
Many languages have standard repositories where people donate useful libraries that they want others
I have a standard textbox and I've got jQuery on the page. I want
I have a standard associative array in PHP. what's the simplest way to get
I have a standard java array, where null values are used for unassigned or
I have a standard deck of 52 cards is represented in an array. Each
Populate php array into HTML table I have data in a php array (say,
So I have a C++ application that takes a value from a key in
I've heard they have standard GUI guideline, but I can't seem to find a
I have a standard windows server that inherits from the ServiceBase class. On the

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.