title: Creating parent-child array PHP
body: <p>I have this flat array that I am trying to turn into a parent-child array: </p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze7b2e
          )
  
      [1] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czeg8ay
          )
  
      [2] =&gt; Array
          (
              [parent_id] =&gt; t1_czeg8ay
              [id] =&gt; t1_czet481
          )
  
      [3] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze4whs
          )
  
      [4] =&gt; Array
          (
              [parent_id] =&gt; t1_cze4whs
              [id] =&gt; t1_cze9v0o
          )
  
      [5] =&gt; Array
          (
              [parent_id] =&gt; t1_cze9v0o
              [id] =&gt; t1_czec4vd
          )
  
      [6] =&gt; Array
          (
              [parent_id] =&gt; t1_czec4vd
              [id] =&gt; t1_czep9e2
          )
  
      [7] =&gt; Array
          (
              [parent_id] =&gt; t1_czep9e2
              [id] =&gt; t1_czf2k8e
          )
  
      [8] =&gt; Array
          (
              [parent_id] =&gt; t1_czec4vd
              [id] =&gt; t1_czeixa7
          )
  
      [9] =&gt; Array
          (
              [parent_id] =&gt; t1_cze9v0o
              [id] =&gt; t1_czecx26
          )
  
      [10] =&gt; Array
          (
              [parent_id] =&gt; t1_czecx26
              [id] =&gt; t1_czeenuz
          )
  
      [11] =&gt; Array
          (
              [parent_id] =&gt; t1_czeenuz
              [id] =&gt; t1_czeftkf
          )
  
      [12] =&gt; Array
          (
              [parent_id] =&gt; t1_czecx26
              [id] =&gt; t1_czehjrz
          )
  
      [13] =&gt; Array
          (
              [parent_id] =&gt; t1_czecx26
              [id] =&gt; t1_czeien4
          )
  
      [14] =&gt; Array
          (
              [parent_id] =&gt; t1_cze9v0o
              [id] =&gt; t1_czebugl
          )
  
      [15] =&gt; Array
          (
              [parent_id] =&gt; t1_cze4whs
              [id] =&gt; t1_cze9hvb
          )
  
      [16] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze3vku
          )
  
      [17] =&gt; Array
          (
              [parent_id] =&gt; t1_cze3vku
              [id] =&gt; t1_czemk0g
          )
  
      [18] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czeedh9
          )
  
      [19] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czeenjx
          )
  
      [20] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze5r5u
          )
  
      [21] =&gt; Array
          (
              [parent_id] =&gt; t1_cze5r5u
              [id] =&gt; t1_czefle4
          )
  
      [22] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze8oht
          )
  
      [23] =&gt; Array
          (
              [parent_id] =&gt; t1_cze8oht
              [id] =&gt; t1_czelwvs
          )
  
      [24] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze5cs6
          )
  
      [25] =&gt; Array
          (
              [parent_id] =&gt; t1_cze5cs6
              [id] =&gt; t1_czefydi
          )
  
      [26] =&gt; Array
          (
              [parent_id] =&gt; t1_cze5cs6
              [id] =&gt; t1_czedpml
          )
  
      [27] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze42aq
          )
  
      [28] =&gt; Array
          (
              [parent_id] =&gt; t1_cze42aq
              [id] =&gt; t1_cze8iei
          )
  
      [29] =&gt; Array
          (
              [parent_id] =&gt; t1_cze8iei
              [id] =&gt; t1_czebkgk
          )
      [30] =&gt; Array
          (
              [parent_id] =&gt; t1_czebkgk
              [id] =&gt; t1_czedid2
          )
      [31] =&gt; Array
          (
              [parent_id] =&gt; t1_cze8iei
              [id] =&gt; t1_czebqgn
          )
      [32] =&gt; Array
          (
              [parent_id] =&gt; t1_cze42aq
              [id] =&gt; t1_cze77xr
          )
      [33] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czen20j
          )
  )
  </code></pre>
  
  <p>Here is my recursive function that maps the ids and makes it into a parent-child array: </p>
  
  <pre><code>function buildTree(array $elements, $parentId = 0) {
      $branch = array();
  
      foreach ($elements as $element) {
          if ($element['parent_id'] == $parentId) {
              $children = buildTree($elements, $element['id']);
              if ($children) {
                  $element['children'] = $children;
              }
              $branch[] = $element;
          }
      }
  
      return $branch;
  }
  </code></pre>
  
  <p>Here is the output I am getting from the above function: </p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze7b2e
          )
  
      [1] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czeg8ay
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czeg8ay
                              [id] =&gt; t1_czet481
                          )
  
                  )
  
          )
  
      [2] =&gt; Array
          (
              [parent_id] =&gt; t1_czeg8ay
              [id] =&gt; t1_czet481
          )
  
      [3] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze4whs
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze4whs
                              [id] =&gt; t1_cze9v0o
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_cze9v0o
                                              [id] =&gt; t1_czec4vd
                                              [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czec4vd
                                                              [id] =&gt; t1_czep9e2
                                                              [children] =&gt; Array
                                                                  (
                                                                      [0] =&gt; Array
                                                                          (
                                                                              [parent_id] =&gt; t1_czep9e2
                                                                              [id] =&gt; t1_czf2k8e
                                                                          )
  
                                                                  )
  
                                                          )
  
                                                      [1] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czec4vd
                                                              [id] =&gt; t1_czeixa7
                                                          )
  
                                                  )
  
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_cze9v0o
                                              [id] =&gt; t1_czecx26
                                              [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czecx26
                                                              [id] =&gt; t1_czeenuz
                                                              [children] =&gt; Array
                                                                  (
                                                                      [0] =&gt; Array
                                                                          (
                                                                              [parent_id] =&gt; t1_czeenuz
                                                                              [id] =&gt; t1_czeftkf
                                                                          )
  
                                                                  )
  
                                                          )
  
                                                      [1] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czecx26
                                                              [id] =&gt; t1_czehjrz
                                                          )
  
                                                      [2] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czecx26
                                                              [id] =&gt; t1_czeien4
                                                          )
  
                                                  )
  
                                          )
  
                                      [2] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_cze9v0o
                                              [id] =&gt; t1_czebugl
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze4whs
                              [id] =&gt; t1_cze9hvb
                          )
  
                  )
  
          )
  
      [4] =&gt; Array
          (
              [parent_id] =&gt; t1_cze4whs
              [id] =&gt; t1_cze9v0o
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze9v0o
                              [id] =&gt; t1_czec4vd
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czec4vd
                                              [id] =&gt; t1_czep9e2
                                              [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czep9e2
                                                              [id] =&gt; t1_czf2k8e
                                                          )
  
                                                  )
  
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czec4vd
                                              [id] =&gt; t1_czeixa7
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze9v0o
                              [id] =&gt; t1_czecx26
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czecx26
                                              [id] =&gt; t1_czeenuz
                                              [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czeenuz
                                                              [id] =&gt; t1_czeftkf
                                                          )
  
                                                  )
  
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czecx26
                                              [id] =&gt; t1_czehjrz
                                          )
  
                                      [2] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czecx26
                                              [id] =&gt; t1_czeien4
                                          )
  
                                  )
  
                          )
  
                      [2] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze9v0o
                              [id] =&gt; t1_czebugl
                          )
  
                  )
  
          )
  
      [5] =&gt; Array
          (
              [parent_id] =&gt; t1_cze9v0o
              [id] =&gt; t1_czec4vd
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czec4vd
                              [id] =&gt; t1_czep9e2
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czep9e2
                                              [id] =&gt; t1_czf2k8e
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czec4vd
                              [id] =&gt; t1_czeixa7
                          )
  
                  )
  
          )
  
      [6] =&gt; Array
          (
              [parent_id] =&gt; t1_czec4vd
              [id] =&gt; t1_czep9e2
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czep9e2
                              [id] =&gt; t1_czf2k8e
                          )
  
                  )
  
          )
  
      [7] =&gt; Array
          (
              [parent_id] =&gt; t1_czep9e2
              [id] =&gt; t1_czf2k8e
          )
  
      [8] =&gt; Array
          (
              [parent_id] =&gt; t1_czec4vd
              [id] =&gt; t1_czeixa7
          )
  
      [9] =&gt; Array
          (
              [parent_id] =&gt; t1_cze9v0o
              [id] =&gt; t1_czecx26
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czecx26
                              [id] =&gt; t1_czeenuz
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czeenuz
                                              [id] =&gt; t1_czeftkf
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czecx26
                              [id] =&gt; t1_czehjrz
                          )
  
                      [2] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czecx26
                              [id] =&gt; t1_czeien4
                          )
  
                  )
  
          )
  
      [10] =&gt; Array
          (
              [parent_id] =&gt; t1_czecx26
              [id] =&gt; t1_czeenuz
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czeenuz
                              [id] =&gt; t1_czeftkf
                          )
  
                  )
  
          )
  
      [11] =&gt; Array
          (
              [parent_id] =&gt; t1_czeenuz
              [id] =&gt; t1_czeftkf
          )
  
      [12] =&gt; Array
          (
              [parent_id] =&gt; t1_czecx26
              [id] =&gt; t1_czehjrz
          )
  
      [13] =&gt; Array
          (
              [parent_id] =&gt; t1_czecx26
              [id] =&gt; t1_czeien4
          )
  
      [14] =&gt; Array
          (
              [parent_id] =&gt; t1_cze9v0o
              [id] =&gt; t1_czebugl
          )
  
      [15] =&gt; Array
          (
              [parent_id] =&gt; t1_cze4whs
              [id] =&gt; t1_cze9hvb
          )
  
      [16] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze3vku
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze3vku
                              [id] =&gt; t1_czemk0g
                          )
  
                  )
  
          )
  
      [17] =&gt; Array
          (
              [parent_id] =&gt; t1_cze3vku
              [id] =&gt; t1_czemk0g
          )
  
      [18] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czeedh9
          )
  
      [19] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czeenjx
          )
  
      [20] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze5r5u
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze5r5u
                              [id] =&gt; t1_czefle4
                          )
  
                  )
  
          )
  
      [21] =&gt; Array
          (
              [parent_id] =&gt; t1_cze5r5u
              [id] =&gt; t1_czefle4
          )
  
      [22] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze8oht
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze8oht
                              [id] =&gt; t1_czelwvs
                          )
  
                  )
  
          )
  
      [23] =&gt; Array
          (
              [parent_id] =&gt; t1_cze8oht
              [id] =&gt; t1_czelwvs
          )
  
      [24] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze5cs6
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze5cs6
                              [id] =&gt; t1_czefydi
                          )
  
                      [1] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze5cs6
                              [id] =&gt; t1_czedpml
                          )
  
                  )
  
          )
  
      [25] =&gt; Array
          (
              [parent_id] =&gt; t1_cze5cs6
              [id] =&gt; t1_czefydi
          )
  
      [26] =&gt; Array
          (
              [parent_id] =&gt; t1_cze5cs6
              [id] =&gt; t1_czedpml
          )
  
      [27] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_cze42aq
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze42aq
                              [id] =&gt; t1_cze8iei
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_cze8iei
                                              [id] =&gt; t1_czebkgk
                                              [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [parent_id] =&gt; t1_czebkgk
                                                              [id] =&gt; t1_czedid2
                                                          )
  
                                                  )
  
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_cze8iei
                                              [id] =&gt; t1_czebqgn
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze42aq
                              [id] =&gt; t1_cze77xr
                          )
  
                  )
  
          )
  
      [28] =&gt; Array
          (
              [parent_id] =&gt; t1_cze42aq
              [id] =&gt; t1_cze8iei
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze8iei
                              [id] =&gt; t1_czebkgk
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [parent_id] =&gt; t1_czebkgk
                                              [id] =&gt; t1_czedid2
                                          )
  
                                  )
  
                          )
  
                      [1] =&gt; Array
                          (
                              [parent_id] =&gt; t1_cze8iei
                              [id] =&gt; t1_czebqgn
                          )
  
                  )
  
          )
  
      [29] =&gt; Array
          (
              [parent_id] =&gt; t1_cze8iei
              [id] =&gt; t1_czebkgk
              [children] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [parent_id] =&gt; t1_czebkgk
                              [id] =&gt; t1_czedid2
                          )
  
                  )
  
          )
  
      [30] =&gt; Array
          (
              [parent_id] =&gt; t1_czebkgk
              [id] =&gt; t1_czedid2
          )
  
      [31] =&gt; Array
          (
              [parent_id] =&gt; t1_cze8iei
              [id] =&gt; t1_czebqgn
          )
  
      [32] =&gt; Array
          (
              [parent_id] =&gt; t1_cze42aq
              [id] =&gt; t1_cze77xr
          )
  
      [33] =&gt; Array
          (
              [parent_id] =&gt; t3_42yrg7
              [id] =&gt; t1_czen20j
          )
  
  )
  </code></pre>
  
  <p>While the first part of it is correct, it sometimes labels the child arrays as a parent after mapping it correctly the first time. Why are some child arrays being mapped as a parent?</p>
  
  <p><strong>Edit:</strong> I'm an idiot, when I calling <code>buildTree()</code> I was not passing in a parentId and just left the value as zero.</p>