title: How to create nested php arrays recursively?
body: <p>I have some data in this format:</p>
  
  <pre><code>one,one
  two,two
  sub_one,one
  sub_two,two
  sub_sub_one,sub_one
  sub_sub_two,sub_two
  sub_sub_sub_one,sub_sub_one
  sub_sub_sub_two,sub_sub_two
  </code></pre>
  
  <p>Now I want to create nested arrays that contains that data, I coded this:</p>
  
  <pre><code>&lt;?php
  $lines[] = "one,one";
  $lines[] = "two,two";
  $lines[] = "sub_one,one";
  $lines[] = "sub_two,two";
  $lines[] = "sub_sub_one,sub_one";
  $lines[] = "sub_sub_two,sub_two";
  $lines[] = "sub_sub_sub_one,sub_sub_one";
  $lines[] = "sub_sub_sub_two,sub_sub_two";
  
  foreach($lines as $line)
  {
      $tmp = explode(",", $line);
      $array[$tmp[1]][$tmp[0]] = $tmp[0];
  }
  
  foreach($array as $key =&gt; $value)
  {
      foreach($array[$key] as $value2)
      {
          if(array_key_exists($value2, $array) &amp;&amp; $value2 != $key)
          {           
              $array[$key][$value2] = $array[$value2];
              $unset[] = $value2;
          }
          else
          {
              unset($array[$key]);
          }
      }
  }
  
  foreach($unset as $un)
  {
      unset($array[$un]);
  }
  
  print_r($array);
  ?&gt;
  </code></pre>
  
  <p>But this only goes down to the 3rd level and no futher. The output looks like this:</p>
  
  <pre><code>Array
  (
      [one] =&gt; Array
          (
              [sub_one] =&gt; Array
                  (
                      [sub_sub_one] =&gt; sub_sub_one
                  )
  
          )
  
      [two] =&gt; Array
          (
              [sub_two] =&gt; Array
                  (
                      [sub_sub_two] =&gt; sub_sub_two
                  )
  
          )
  
  )
  </code></pre>
  
  <p><strong>sub_sub_sub_one</strong> and <strong>sub_sub_sub_two</strong> are not in the output, how can I make my code recursive so no matter how many levels or relation exists in the data it will still work?</p>