title: List to tree structure
body: <p>I have the following sample array list;</p>
  
  <pre><code>$relationships = ['contact', 'property', 'contact.type1', 'contact.type2', 'contact.type1.color1', 'contact.type1.color2'];
  </code></pre>
  
  <p>I want a structure like this;</p>
  
  <pre><code>[
      contact =&gt; [ 
                   type1 =&gt; [
                               color1 =&gt; [], 
                               color2 =&gt; []
                            ], 
                   type2 =&gt; [] 
                 ], 
      property =&gt; []
  ]
  </code></pre>
  
  <p>I created the following code;</p>
  
  <pre><code>start();
  
  function start()
  {
      $myTree = array();
  
      $relations = ['contact', 'property', 'contact.type1', 'contact.type2', 'contact.type1.color1', 'contact.type1.color2'];
  
      foreach ($relations as $relation) {
  
          $myTree = getRelationTree($relation, $myTree);
      }
  
      var_dump($myTree);
  }
  
  function getRelationTree($name, $tree)
  {
      $sections = explode('.', $name);
  
      $main = array_shift($sections);
  
      if (!array_search($main, $tree)) {
          $tree[$main] = [] ;
      }
  
      // Has nested relationships
      if (count($sections) &gt; 0) {
          $children  = [];
          $children[] = getRelationTree(join('.',$sections), $children);
  
          $tree[$main] = $children;
      }
  
      return $tree;
  }
  </code></pre>
  
  <p>However, the results I get is missing <code>color1</code> as per expected results above.</p>
  
  <p>Could you tell me what's wrong with my code and how I can fix it to give me the desired results? Thanks</p>