title: Algorithm for creating multi-dimensional array
body: <p>I'm using PHP and I need help with a seemingly simple task with an array.</p>
  
  <p>This is my example array:</p>
  
  <pre><code>$arr = array(
      0  =&gt; NULL,
      1  =&gt; NULL,
      2  =&gt; NULL,
      3  =&gt; NULL,
      8  =&gt; '2',
      9  =&gt; '2',
      10 =&gt; '2',
      11 =&gt; '2',
      12 =&gt; '3',
      13 =&gt; '3',
      14 =&gt; '8',
      15 =&gt; '8',
      16 =&gt; '14',
      17 =&gt; '14',
      18 =&gt; '14'
  );
  </code></pre>
  
  <p>The keys of the array represent the <strong>IDs</strong> (unique).<br>
  The values are the <strong>parentIDs</strong>, i.e. the ID of the parent "node". <code>NULL</code> means that there is no parentID (i.e. 1st dimension of the new array).</p>
  
  <p>Now, I need to create a new, multi-dimensional array that has all the child elements under their parent IDs. (This probably sounds very confusing, sorry for my lack of descriptive abilities. There's an example below, which should make things clearer)</p>
  
  <p>Here's what the new array of my example would look like after the "sorting" function, or whatever you call this, was applied:</p>
  
  <pre>
  $arr = array(
   0 => array(),
   1 => array(),
   2 => array(
       8 => array(
          14 => array(
              16 => array(),
              17 => array(),
              18 => array()
  ),
          15 => array()
  ),
       9 => array(),
      10 => array(),
      11 => array()
  ),
   3 => array(
      12 => array(),
      13 => array()
  )
  );
  </pre>
  
  <p>I know all the empty array()s are probably not a very clean and elegant solution but unfortunately this is the way I need it to be!</p>