title: PHP - replace array keys recursively and change values to array including old key
body: <p>I've spent a while trying to get what I need from old answers but haven't quite got it (have got close though!).</p>
  
  <p>I have this;</p>
  
  <pre><code>[January] =&gt; Array
      (
          [Tuesday] =&gt; Array
              (
                  [foo] =&gt; Array
                      (
                          [82] =&gt; 47731
                          [125] =&gt; 19894
                      )
  
                  [bar] =&gt; Array
                      (
                          [82] =&gt; 29911
                          [125] =&gt; 10686
                      )
  
              )
  
      }
  </code></pre>
  
  <p>...and I want this;</p>
  
  <pre><code>[0] =&gt; Array
      (
         'key' =&gt; 'January'
         'children' =&gt; Array
              [0] =&gt; Array 
                  {
                      'key' =&gt; 'Tuesday'
                      'children' =&gt; Array 
                           [0] =&gt; Array 
                                {
                                    'key' =&gt; 'foo'
                                    'values' =&gt; Array
                                          {
                                               [82] =&gt; 47731
                                               [125] =&gt; 19894
                                          }
                           [1] =&gt; Array
                                {
                                    'key' =&gt; 'bar'
                                    'values' =&gt; Array
                                          {
                                               [82] =&gt; 29911
                                               [125] =&gt; 10686
                                          }
                                }
                  )
      }
  </code></pre>
  
  <p>I've got fairly close by adapting the first answer from <a href="https://stackoverflow.com/questions/11993613/recursively-change-keys-in-array">Recursively change keys in array</a> but only the bottom layer of my result is correct - the nodes with keys 'Tuesday', 'foo' and 'bar' just look the same as in the source array.</p>
  
  <p>Here's what I've got so far;</p>
  
  <pre><code>public function transform_hierarchical_output(&amp;$var)
  {       
      if (is_array($var)) 
      {
          $final = [];
  
          $i = 0;
  
          foreach ($var as $k =&gt; &amp;$v) 
          {
              $new_node = [
                  'key'       =&gt; $k,
                  'children'  =&gt; $v
              ];
  
              $k = $i;
  
              $this-&gt;transform_hierarchical_output($v);
  
              $final[$k] = $new_node;
  
              $i++;
          }
  
          $var = $final;
      } 
      elseif (is_string($var)) 
      {
  
      }
  }
  </code></pre>
  
  <p>This needs to work with a source array of any length and depth.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Geoff</p>