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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:21:11+00:00 2026-05-24T17:21:11+00:00

I need to convert a flat array where the array keys indicate the structure

  • 0

I need to convert a flat array where the array keys indicate the structure into a nested array where the parent element becomes element zero, i.e. in the example:

$education['x[1]'] = 'Georgia Tech';

It needs to be converted to:

$education[1][0] = 'Georgia Tech';

Here is an example input array:

$education = array(
  'x[1]'     => 'Georgia Tech',
  'x[1][1]'  => 'Mechanical Engineering',
  'x[1][2]'  => 'Computer Science',
  'x[2]'     => 'Agnes Scott',
  'x[2][1]'  => 'Religious History',
  'x[2][2]'  => 'Women\'s Studies',
  'x[3]'     => 'Georgia State',
  'x[3][1]'  => 'Business Administration',
);

And here is what the output should be:

$education => array(
  1 => array(
    0 => 'Georgia Tech',
    1 => array( 0 => 'Mechanical Engineering' ),
    2 => array( 0 => 'Computer Science' ),
  ),
  2 => array(
    0 => 'Agnes Scott',
    1 => array( 0 => 'Religious History' ),
    2 => array( 0 => 'Women\'s Studies' ),
  ),
  3 => array(
    0 => 'Georgia State',
    1 => array( 0 => 'Business Administration' ),
  ),
);

I’ve banged my head against the wall for hours and still can’t get it working. I think I’ve been looking at it too long. Thanks in advance.

P.S. It should be fully nestable, i.e. it should be able to convert a key that looks like this:

x[1][2][3][4][5][6] 

P.P.S. @Joseph Silber had a clever solution but unfortunately using eval() is not an option for this as it’s a WordPress plugin and the WordPress community is trying to stamp out the use of eval().

  • 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-24T17:21:12+00:00Added an answer on May 24, 2026 at 5:21 pm

    Here is some code to handle what you had originally proposed as output.

    /**
     * Give it and array, and an array of parents, it will decent into the
     * nested arrays and set the value.
     */
    function set_nested_value(array &$arr, array $ancestors, $value) {
      $current = &$arr;
      foreach ($ancestors as $key) {
    
        // To handle the original input, if an item is not an array, 
        // replace it with an array with the value as the first item.
        if (!is_array($current)) {
          $current = array( $current);
        }
    
        if (!array_key_exists($key, $current)) {
          $current[$key] = array();
        }
        $current = &$current[$key];
      }
    
      $current = $value;
    }
    
    
    $education = array(
      'x[1]'     => 'Georgia Tech',
      'x[1][1]'  => 'Mechanical Engineering',
      'x[1][2]'  => 'Computer Science',
      'x[2]'     => 'Agnes Scott',
      'x[2][1]'  => 'Religious History',
      'x[2][2]'  => 'Women\'s Studies',
      'x[3]'     => 'Georgia State',
      'x[3][1]'  => 'Business Administration',
    );
    
    $neweducation = array();
    
    foreach ($education as $path => $value) {
      $ancestors = explode('][', substr($path, 2, -1));
      set_nested_value($neweducation, $ancestors, $value);
    }
    

    Basically, split your array keys into a nice array of ancestor keys, then use a nice function to decent into the $neweducation array using those parents, and set the value.

    If you want the output that you have updated your post to have, add this in the foreach loop after the line with ‘explode’.

    $ancestors[] = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PROBLEM I have a nested PHP array that I need to populate from flat
I need to convert a flat file to DB using MS SSIS. I need
I need to convert latitude/longitude coordinates into Easting/Northing coordinates in the Alberta 10 TM
I need to convert HTML documents into valid XML, preferably XHTML. What's the best
I need to convert a (possibly) null terminated array of ascii bytes to a
I need to convert a value which is in a DateTime variable into a
I need to convert a Word document into HTML file(s) in Java. The function
I need to convert an arbitrary amount of milliseconds into Days, Hours, Minutes Second.
I'm trying to convert a flat file into an Excel-readable format. We receive the
I need to convert a navigable map to a 2D String array. Below given

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.