title: Convert associative array to multidimensional array with value
body: <p>This is an interesting situation which I created a working function for, but wondering if I just anyone had any simpler methods for this.  </p>
  
  <p>I have the following multidimensional array:</p>
  
  <pre><code>$foo = array(
      [0] =&gt; array(
          'keys' =&gt; array( 
              'key1' =&gt; 1,
              'key2' =&gt; a,
              'key3' =&gt; 123
          ),
         'values' =&gt; array(
              //goodies in here
          )
      )
      [1] =&gt; array(
          'keys' =&gt; array( 
              'key1' =&gt; 1,
              'key2' =&gt; b,
              'key3' =&gt; 456
          ),
         'values' =&gt; array(
              //goodies in here
          )
      )
  )
  </code></pre>
  
  <p>What I wanted, was to transform this into a multidimensional array nested based on the <strong>values</strong> from the <code>keys</code> array, the output I was looking for is:</p>
  
  <pre><code>$bar = array(
      [1] =&gt; array(
          [a] =&gt; array(
              [123] =&gt; array( //values array from above )
          ),
          [b] =&gt; array(
              [456] =&gt; array( //values array from above )
          )
      )
  )
  </code></pre>
  
  <p>The keys can always be nested based on their position in the <code>keys</code> array, but the keys themselve are not always the same, <code>keys</code> handles a user defined grouping, so the order and values can change.  I also didn't want duplicate keys. </p>
  
  <p><code>array_merge</code> 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 <a href="http://thedailywtf.com/Articles/The_Complicator_0x27_s_Gloves.aspx" rel="nofollow" title="gloves">gloves</a>.</p>
  
  <pre><code>   protected function convertAssociativeToMulti( &amp;$output, $keys, $value )
     {
        $temp = array();
        $v = array_values( $keys );
        $s = sizeof( $v );
        for( $x = 0; $x &lt; $s; $x++ )
        {
           $k = $v[ $x ];
           if ( $x == 0 )
           {
              if ( !array_key_exists( $k, $output ) )
                 $output[ $k ] = array();
              $temp =&amp; $output[ $k ];
           }
           if ( $x &amp;&amp; ( $x + 1 ) !== $s )
           {
              if( !array_key_exists( $k, $temp ) )
                 $temp[ $k ] = array();
              $temp =&amp; $temp[$k];
           }
           if ( ( $x + 1 ) == $s )
              $temp[$k] = $value;
        }
     }
  
     $baz = array();
     foreach( $foo as $bar )
     {
        $this-&gt;convertAssociativeToMulti( $baz, $bar['keys'], $bar['values'] );
     }
  </code></pre>
  
  <p>So, how do you do this more simply / refactor what I have?</p>