title: How to recursively loop through json to create parent/child objects PHP
body: <p>I have a json file that has an array called "dialogue_nodes". Each object in the array has a "parent" value and a "dialogue_node" value. If a dialogue node has a value for parent, the value references another dialogue node's "dialogue_node" value. In the json file, all of the parent and child nodes are on the same "level" in the top dialogue_nodes array. I'm currently manually looping through the array. </p>
  
  <p>How do I dynamically loop through the array to account for the indefinite levels of the parent/child relationships? Here is my current code.</p>
  
  <pre><code>$content = file_get_contents( "testWorkspace.json" );
  
  $json = json_decode( $content );
  $dialogueNodes = $json-&gt;dialog_nodes;
  
  foreach ( $dialogueNodes as $node ) {
  
     if ( !isset( $node-&gt;parent ) ) {
  
         $topNodes[] = $node;
  
     } else {
  
      $otherNodes[] = $node;
     }
  }
  
  foreach ( $topNodes as $node ) {
  
     echo "top: {$node-&gt;title}";
  
     foreach ( $otherNodes as $node2 ) {
  
      if ( $node2-&gt;parent == $node-&gt;dialog_node ) {
  
          echo "level 2 child is {$node2-&gt;dialog_node} {$node2-&gt;title}";
  
          foreach ( $otherNodes as $node3 ) {
  
              if ( $node3-&gt;parent == $node2-&gt;dialog_node ) {
  
                  echo "level 3 child is {$node3-&gt;dialog_node} {$node3-&gt;title}";
  
   continues.....
  </code></pre>
  
  <p>Here is a sample input. The first object is a parent node example, the second is a child node</p>
  
  <pre><code>{
    "title": "Welcome",
    "dialog_node": "node_5_1550586774524",
  },
  {
    "condition": true
    "output": "hello"
    "parent": "node_5_1550586774524"
    "dialog_node": "node_12_1554909604222"
  },
  </code></pre>