Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert associative array to multidimensional array with value

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?

like image 954
kmfk Avatar asked Jan 25 '13 02:01

kmfk


People also ask

What is the difference between the associative array and multidimensional array?

Associative array — An array where each key has its own specific value. Multidimensional array — An array containing one or more arrays within itself.

What is multidimensional associative array?

PHP Multidimensional array is used to store an array in contrast to constant values. Associative array stores the data in the form of key and value pairs where the key can be an integer or string. Multidimensional associative array is often used to store data in group relation.

What is the difference between associative array and array?

The index data type for a simple array must be an integer value. The index type for an associative array can be one of a set of supported data types. The index values in a simple array must be a contiguous set of integer values. In an associative array the index values can be sparse.

How do you find the associative value of an array?

You can use the PHP array_values() function to get all the values of an associative array.


1 Answers

This is a bit more concise (See it in action here):

$bar=array();
foreach($foo as $item)
{
    $out=&$bar;
    foreach($item['keys'] as $key)
    {
        if(!isset($out[$key])) $out[$key]=array();
        $out=&$out[$key];
    }
    foreach($item['values'] as $k=>$v) $out[$k]=$v;
//    $out=array_merge($out,$item['values']); -- edit - fixed array_merge
}
var_dump($bar);
like image 131
Jon Hulka Avatar answered Sep 30 '22 18:09

Jon Hulka