title: Looping through categories and build a category tree using the parent id
body: <p>I am trying to work with a database containing category information. The relation between the items is only present in form of a parent_id.
  I know I could do some "workarounds" to reach the goal, but there has to be a better solution. I could write some more info into the database, like deepness, but I am sure there is a loop/array_filter combination I am not aware about.</p>
  
  <p><strong>This is what I have:</strong></p>
  
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [id] =&gt; 0
          [parent_id] =&gt; 0
          [title] =&gt; All
      )
  
  [1] =&gt; Array
      (
          [id] =&gt; 23
          [parent_id] =&gt; 0
          [title] =&gt; Nature
      )
  
  [2] =&gt; Array
      (
          [id] =&gt; 3
          [parent_id] =&gt; 4
          [title] =&gt; Thriller
      )
  
  [3] =&gt; Array
      (
          [id] =&gt; 4
          [parent_id] =&gt; 0
          [title] =&gt; Drama
      )
  
  [4] =&gt; Array
      (
          [id] =&gt; 1
          [parent_id] =&gt; 0
          [title] =&gt; Crime
      )
  
  [5] =&gt; Array
      (
          [id] =&gt; 19
          [parent_id] =&gt; 23
          [title] =&gt; Documentation
      )
  
  [6] =&gt; Array
      (
          [id] =&gt; 20
          [parent_id] =&gt; 23
          [title] =&gt; Coverage
      )
  
  [7] =&gt; Array
      (
          [id] =&gt; 21
          [parent_id] =&gt; 19
          [title] =&gt; Isles
      )
  </code></pre>
  
  <p><strong>This is what I want:</strong></p>
  
  <pre><code>Array
  (
  [0] =&gt; Array
      (
          [id] =&gt; 0
          [parent_id] =&gt; 0
          [title] =&gt; All
          [0] =&gt; Array
              (
                  [id] =&gt; 23
                  [parent_id] =&gt; 0
                  [title] =&gt; Nature
                  [0] =&gt; Array
                          (
                              [id] =&gt; 19
                              [parent_id] =&gt; 23
                              [title] =&gt; Documentation
                              [0] =&gt; Array
                                      (
                                          [id] =&gt; 21
                                          [parent_id] =&gt; 19
                                          [title] =&gt; Isles
                                      )
                          )
                  [1] =&gt; Array
                          (
                              [id] =&gt; 20
                              [parent_id] =&gt; 23
                              [title] =&gt; Coverages
                          )
                  )
  
          [1] =&gt; Array
              (
                  [id] =&gt; 4
                  [parent_id] =&gt; 0
                  [title] =&gt; Drama
                  [0] =&gt; Array
                          (
                              [id] =&gt; 3
                              [parent_id] =&gt; 4
                              [title] =&gt; Thriller
                          )
              )
  
          [2] =&gt; Array
              (
                  [id] =&gt; 1
                  [parent_id] =&gt; 0
                  [title] =&gt; Crime
              )
      )
  </code></pre>
  
  <p>I tried it with loops and array_filter() but it was not working. I tried it with a loop within a function and calling the function within the same loop, but it didn´t work. Then I started trying it with only 1 subcategory, but this can´t be a solution nor a workaround.</p>
  
  <pre><code>$categories = [see first code block]
  $i = -1;
  $j = -1;
  
  foreach ($categories as $cat) {
      $i++;
      if ($cat['parent_id'] &gt; 0) {
          $j = -1;
          foreach ($categories as $cat2) {
              $j++;
              if ($cat2['id'] == $cat['parent_id']) {
                  $categories[$j][] = array("id" =&gt; $cat['id'], "parent_id" =&gt; $cat['parent_id'], "title" =&gt; $cat['title']);
                  unset($categories[$i]);
                  continue 2;
              }
          }
      }
  }
  </code></pre>
  
  <p>I would be happy for every push in the right direction. Thanks!</p>