title: Struggling to convert an array structure to a recursive tree in PHP
body: <p>I've been pulling my hair out now for a few hours trying to convert a flat array that contains categories into a multi-level tree, but I'm struggling to write a function that can do it, I've been trying for around 4 hours now and I'm at my wits end.</p>
  
  <p>I'm pretty new to Algorithms and working with this kind of data structure so I'm hoping someone can point me in the right direction (I've tried several solutions listed here on SO to no avail).</p>
  
  <p>Most seem to require an id field of sorts but unfortunately my data structure does not have one.</p>
  
  <p>The original data structure looks like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>        $data = [
              ['name'=&gt;"vehicles",'parent'=&gt;'root'],
              ['name'=&gt;"cars",'parent'=&gt;'vehicles'],
              ['name'=&gt;"Porsche",'parent'=&gt;'cars'],
              ['name'=&gt;"Volvo",'parent'=&gt;'cars'],
              ['name'=&gt;"Ford",'parent'=&gt;'cars'],
              ['name'=&gt;"Skoda",'parent'=&gt;'cars'],
              ['name'=&gt;"Kawasaki",'parent'=&gt;'bikes'],
              ['name'=&gt;"Harley",'parent'=&gt;'bikes'],
              ['name'=&gt;"Electronics",'parent'=&gt;'root'],
              ['name'=&gt;"Mobile Phones",'parent'=&gt;'Electronics'],
              ['name'=&gt;"Nokia",'parent'=&gt;'Mobile Phones'],
              ['name'=&gt;"Apple",'parent'=&gt;'Mobile Phones'],
              ['name'=&gt;"Motorola",'parent'=&gt;'Mobile Phones'],
              ['name'=&gt;"Google",'parent'=&gt;'Mobile Phones']
          ];
  </code></pre>
  
  <p>And I'm trying to make the output look like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>        $output=[
              [
                  'name'=&gt;"root",
                  'children'=&gt;[
                      [
                          "name"=&gt;"vehicles",
                          "children"=&gt;[
                              [
                                  "name"=&gt;"cars",
                                  "children"=&gt;[
                                      ["name"=&gt;"Porsche"],
                                      ["name"=&gt;"Volvo"],
                                      ["name"=&gt;"Ford"],
                                      ["name"=&gt;"Skoda"],
                                  ]
                              ],
                          ]
                      ],
                      [
                          "name"=&gt;"Electronics",
                          "children"=&gt;[
                              [
                                  "name"=&gt;"Mobile Phones",
                                  "children"=&gt;[
                                      ["name"=&gt;"Nokia"],
                                      ["name"=&gt;"Apple"],
                                      ["name"=&gt;"Google"],
                                      ["name"=&gt;"Motorola"],
                                  ]
  
                              ]
  
                          ]
                      ]
                  ]]
          ];
  
  </code></pre>
  
  <p>If someone could point me in the right direction I'd be very grateful... Hell, at this point I'd even pay for the solution.</p>
  
  <p>EDIT: I found a solution, thanks to everyone who helped me out.</p>
  
  <pre class="lang-php prettyprint-override"><code>
      function buildTree(array $elements, $parentId = 0)
      {
          $branch = array();
          foreach ($elements as $element) {
              if ($element['parent'] == $parentId) {
                  $children = $this-&gt;buildTree($elements, $element['name']);
                  if (!empty($children)) {
                      $element['children'] = $children;
                  }
                  $branch[] = $element;
              }
          }
          return $branch;
      }
  
  </code></pre>