title: How can I copy associative array items to another single array item recursively?
body: <p>I have an associative array with keys that are numbered like software release version numbers, for example <code>32.1</code>, what I want to do is take each of the versions following <code>32.1</code> (32.2, 32.3, 32.4, etc) and assign them to another array under the <code>32</code> key, more so I need the script to be recursive, so that longer version numbers with multiple releases end up in the correct array keys.</p>
  
  <p>My code...</p>
  
  <pre><code>&lt;?php
  
      $array = array(
          '1' =&gt; 'Level 1',
          '1-1' =&gt; 'Level 1',
          '1-2' =&gt; 'Level 1',
          '1-2-1' =&gt; 'Sub-Level 1',
          '2' =&gt; 'Level 2',
          '3' =&gt; 'Level 3'
      );
  
      foreach ($array as $key =&gt; $value) {
          static $assoc = array();
  
          if (preg_match('/-/', $key)) {
              $points = explode('-', $key);
              $assoc_key = basename($key, '-' . end($points));
              $assoc[$assoc_key]['sub'][$key] = array('1' =&gt; 'Sub-' . $array[end($points)]);
          } else {
              $assoc[$key] = array('1' =&gt; $array[$key]);
          }
      }
  
      echo '&lt;pre&gt;' . print_r($assoc, true) . '&lt;/pre&gt;';
  
  ?&gt;
  </code></pre>
  
  <p><strong>Expected Output:</strong></p>
  
  <pre><code>Array
  (
    [1] =&gt; Array
      (
        [1] =&gt; Level 1
        [sub] =&gt; Array
          (
            [1-1] =&gt; Array
              (
                [1] =&gt; Sub-Level 1
              )
  
            [1-2] =&gt; Array
              (
                [1] =&gt; Sub-Level 2
                [sub] =&gt; Array
                  (
                    [1-2-1] =&gt; Array
                      (
                        [1] =&gt; Sub-Sub-Level 1
                      )
  
                  )
  
              )
  
          )
  
      )
  
    [2] =&gt; Array
      (
        [1] =&gt; Level 2
      )
  
    [3] =&gt; Array
      (
        [1] =&gt; Level 3
      )
  
  )
  </code></pre>
  
  <p><strong>Actual Output</strong></p>
  
  <pre><code>Array
  (
    [1] =&gt; Array
      (
        [1] =&gt; Level 1
        [sub] =&gt; Array
          (
            [1-1] =&gt; Array
              (
                [1] =&gt; Sub-Level 1
              )
  
            [1-2] =&gt; Array
              (
                [1] =&gt; Sub-Level 2
              )
  
          )
  
      )
  
    [1-2] =&gt; Array
      (
        [sub] =&gt; Array
          (
            [1-2-1] =&gt; Array
              (
                [1] =&gt; Sub-Level 1
              )
  
          )
  
      )
  
    [2] =&gt; Array
      (
        [1] =&gt; Level 2
      )
  
    [3] =&gt; Array
      (
        [1] =&gt; Level 3
      )
  
  )
  </code></pre>
  
  <p>Can anyone help?</p>
  
  <p>I just know it's something simple I'm missing, but I've been trying to get this to work for two days now.</p>