title: create a tree structure from a set of parent-child relationships
body: <p>I have an array of arrays which holds parent-child relationships between nodes in a graph. Each of the nested arrays is of the form</p>
  
  <pre><code>array( 0 =&gt; parent_node_id, 1 =&gt; child_node_id )
  </code></pre>
  
  <p>So in this array:</p>
  
  <pre><code>0 =&gt; array(
     0 =&gt; 1
     1 =&gt; 3
  )
  </code></pre>
  
  <p>the two nodes are 1 and 3, and there is a parent-child relationship between node 1 and node 3 (the outer array index <code>0</code> is irrelevant).</p>
  
  <pre><code>1 =&gt; array(
     0 =&gt; 3
     1 =&gt; 5
  ),
  </code></pre>
  
  <p>represents a parent-child relationship between node 3 and node 5 (the <code>1</code> is irrelevant).</p>
  
  <p>Here is the parent-child relationship array (note that the array index of the outer array (0, 1, 2, 3, etc.) does not represent anything):</p>
  
  <pre><code>0 =&gt; array(
     0 =&gt; 1
     1 =&gt; 3
  ),
  1 =&gt; array(
     0 =&gt; 3
     1 =&gt; 5
  ),
  2 =&gt; array(
     0 =&gt; 3
     1 =&gt; 7
  ),
  3 =&gt; array(
     0 =&gt; 3
     1 =&gt; 9
  ),
  4 =&gt; array(
     0 =&gt; 1
     1 =&gt; 10
  ),
  5 =&gt; array(
     0 =&gt; 10
     1 =&gt; 15
  )
  </code></pre>
  
  <p>Here is a pictorial representation of the data structure that it encodes:</p>
  
  <p><img src="https://i.stack.imgur.com/PxpFQ.png" alt="enter image description here"></p>
  
  <p>And in code format (although any better ideas for an array structure that I can generate an HTML list from later would be appreciated!):</p>
  
  <pre><code>0 =&gt; array
     0 =&gt; 1
     1 =&gt; array
        0 =&gt; 3
        1 =&gt; array
           0 =&gt; 5
        2 =&gt; array
           0 =&gt; 7
        3 =&gt; array
           0 =&gt; 9
     2 =&gt; array
        0 =&gt; 10
        1 =&gt; array
           0 =&gt; 15
  </code></pre>
  
  <p>Using the information from this array, I would like to generate a tree that I can then use to build a menu in an html page. How can I do this using just my array of parent-child relationships?</p>
  
  <p>I know there are many similar algorithms available on stack overflow, but none that works with multiple roots or the particular array input structure that I am using.</p>