title: How to create array of nested comments out of flat array from DB
body: <p>After querying the DB for comments that are nested in a closure table, like Bill Karwin suggests here <a href="https://stackoverflow.com/questions/192220/what-is-the-most-efficient-elegant-way-to-parse-a-flat-table-into-a-tree/192462#192462">What is the most efficient/elegant way to parse a flat table into a tree?</a>, I now get the following datastructure from SQL:</p>
  
  <pre><code>"comments": [
              {
                  "id": "1",
                  "breadcrumbs": "1",
                  "body": "Bell pepper melon mung."
              },
              {
                  "id": "2",
                  "breadcrumbs": "1,2",
                  "body": "Pea sprouts green bean."
              },
              {
                  "id": "3",
                  "breadcrumbs": "1,3",
                  "body": "Komatsuna plantain spinach sorrel."
              },
              {
                  "id": "4",
                  "breadcrumbs": "1,2,4",
                  "body": "Rock melon grape parsnip."
              },
              {
                  "id": "5",
                  "breadcrumbs": "5",
                  "body": "Ricebean spring onion grape."
              },
              {
                  "id": "6",
                  "breadcrumbs": "5,6",
                  "body": "Chestnut kohlrabi parsnip daikon."
              }
          ]
  </code></pre>
  
  <p>Using PHP I would like to restructure this dataset, so the comments are nested like this:</p>
  
  <pre><code>"comments": [
              {
                  "id": "1",
                  "breadcrumbs": "1",
                  "body": "Bell pepper melon mung."
                  "comments": [
                      {
                          "id": "2",
                          "breadcrumbs": "1,2",
                          "body": "Pea sprouts green bean."
                          "comments": [
                              {
                                  "id": "4",
                                  "breadcrumbs": "1,2,4",
                                  "body": "Rock melon grape parsnip."
                              }
                          ]
                      },
                      {
                          "id": "3",
                          "breadcrumbs": "1,3",
                          "body": "Komatsuna plantain spinach sorrel."
                      }
                  ]
              },
              {
                  "id": "5",
                  "breadcrumbs": "5",
                  "body": "Ricebean spring onion grape."
                  "comments": [
                      {
                          "id": "6",
                          "breadcrumbs": "5,6",
                          "body": "Chestnut kohlrabi parsnip daikon."
                      }
                  ]
              }
          ]
  </code></pre>
  
  <p>I have hacked together a solution, but it seems over complex, and I have a feeling that there is some clever solution out there to do this in an elegant and efficient way, but I dont know how?</p>