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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:20:15+00:00 2026-06-17T20:20:15+00:00

This is an interesting situation which I created a working function for, but wondering

  • 0

This is an interesting situation which I created a working function for, but wondering if I just anyone had any simpler methods for this.

I have the following multidimensional array:

$foo = array(
    [0] => array(
        'keys' => array( 
            'key1' => 1,
            'key2' => a,
            'key3' => 123
        ),
       'values' => array(
            //goodies in here
        )
    )
    [1] => array(
        'keys' => array( 
            'key1' => 1,
            'key2' => b,
            'key3' => 456
        ),
       'values' => array(
            //goodies in here
        )
    )
)

What I wanted, was to transform this into a multidimensional array nested based on the values from the keys array, the output I was looking for is:

$bar = array(
    [1] => array(
        [a] => array(
            [123] => array( //values array from above )
        ),
        [b] => array(
            [456] => array( //values array from above )
        )
    )
)

The keys can always be nested based on their position in the keys array, but the keys themselve are not always the same, keys handles a user defined grouping, so the order and values can change. I also didn’t want duplicate keys.

array_merge failed me because in a lot of cases, the array keys are actually numeric ids. So, this function works – but I’m wondering if I made myself a new pair of gloves.

   protected function convertAssociativeToMulti( &$output, $keys, $value )
   {
      $temp = array();
      $v = array_values( $keys );
      $s = sizeof( $v );
      for( $x = 0; $x < $s; $x++ )
      {
         $k = $v[ $x ];
         if ( $x == 0 )
         {
            if ( !array_key_exists( $k, $output ) )
               $output[ $k ] = array();
            $temp =& $output[ $k ];
         }
         if ( $x && ( $x + 1 ) !== $s )
         {
            if( !array_key_exists( $k, $temp ) )
               $temp[ $k ] = array();
            $temp =& $temp[$k];
         }
         if ( ( $x + 1 ) == $s )
            $temp[$k] = $value;
      }
   }

   $baz = array();
   foreach( $foo as $bar )
   {
      $this->convertAssociativeToMulti( $baz, $bar['keys'], $bar['values'] );
   }

So, how do you do this more simply / refactor what I have?

  • 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-17T20:20:16+00:00Added an answer on June 17, 2026 at 8:20 pm

    This is a bit more concise:

    $result = [];
    foreach($foo as $item)
    {
        //Start at the outermost level of the result array
        $level = &$result;
        foreach($item['keys'] as $key)
        {
            //Add levels to the result array as needed
            if(!isset($level[$key])) $level[$key] = [];
            //Iterate to the next nested level
            $level = &$level[$key];
        }
        //Append values to the innermost level
        foreach($item['values'] as $k => $v) $level[$k] = $v;
    }
    //Don't keep a reference hanging around
    unset($level);
    var_dump($result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with vector now and I have an interesting situation which I need
In searching for an answer to an interesting situation which I had recently encountered
I have an interesting situation where certain things are working but others are not
i have been reading this interesting article which is increasing my every growing confusion
I have an interesting situation, well it's probably not that unique at all, but
I've stumbled upon an interesting situation when object could release itself and was wondering
I came across an interesting situation when using this class: class Company(models.Model): date =
I have an interesting XML situation in which I'm trying to write an XSD
I have an interesting situation - I have a table row which, currently, shows
I just came across an interesting situation in JavaScript. I have a class with

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.