title: php - split string into multidimensional array
body: <p>Im trying to split navigation position strings into a master array.</p>
  
  <p>For example, If I have an item that's position is 1.2.2</p>
  
  <p>I would like to add it in the master array as follows</p>
  
  <pre><code> 1 =&gt;
      2 =&gt;
          2 =&gt; array()
  </code></pre>
  
  <p>And then if another item has '2.1'</p>
  
  <pre><code> 1 =&gt;
      2 =&gt;
          2 =&gt; array()
   2 =&gt;
      1 =&gt; array()
  </code></pre>
  
  <p>and then another '1.2.3'</p>
  
  <pre><code> 1 =&gt;
      2 =&gt;
          2 =&gt; array()
          3 =&gt; array()
   2 =&gt;
      1 =&gt; array()
  </code></pre>
  
  <p>does anyone know of a way for doing this?</p>
  
  <p>regards</p>
  
  <p><strong>edit</strong></p>
  
  <p>lets say I have a one dimensional array of objectects, I want to loop through them and store as a structured "navigation" like nested array. Each item has a navigation position string, i.e. <strong>1.2.3.6</strong></p>
  
  <p>I then was thinking of <code>$depth = explode( '.', $details['navigation_pos'] );</code> running it through some kind of array walker to place the object in the correct position.</p>
  
  <p>hope this helps</p>
  
  <p><strong>edit</strong></p>
  
  <p>maybe a better way to put it is this, but more elegantly:</p>
  
  <pre><code>$depth = explode( '.', '1.2.3.4' );
  $bar = json_decode( '{"' . implode( '":{"', $depth ) . '":[]' . str_repeat( '}', sizeof( $depth ) ) );
  print_r($bar);
  </code></pre>
  
  <p>which would give</p>
  
  <pre><code>stdClass Object
  (
      [1] =&gt; stdClass Object
          (
              [2] =&gt; stdClass Object
                  (
                      [3] =&gt; stdClass Object
                          (
                              [4] =&gt; Array
                                  (
                                  )
  
                          )
  
                  )
  
          )
  
  )
  </code></pre>