title: PHP : Recursively add parant child to the multi dimension array
body: <p>I have a function which gives an array of the below format</p>
  <pre><code>$result = [
        [
          &quot;name&quot; =&gt; &quot;text&quot;,
          &quot;id&quot; =&gt; &quot;928610&quot;,
          &quot;entity_type&quot; =&gt; &quot;node&quot;
        ],
        [
          &quot;name&quot; =&gt; &quot;folder&quot;,
          &quot;id&quot; =&gt; &quot;987620&quot;,
          &quot;entity_type&quot; =&gt; &quot;folder&quot;
        ],
        [
          &quot;name&quot; =&gt; &quot;text&quot;,
          &quot;id&quot; =&gt; &quot;956720&quot;,
          &quot;entity_type&quot; =&gt; &quot;node&quot;
        ],
  ];
  </code></pre>
  <p>Each Folder &quot;entity_type&quot; =&gt; &quot;folder&quot; item has again child which returns same format array.</p>
  <p>like if we run a foreach loop $result and if it is &quot;entity_type&quot; =&gt; &quot;folder&quot; then we pass the id to a function it will also give a similar array format as that of result.</p>
  <p>So i need if it is &quot;entity_type&quot; =&gt; &quot;folder&quot;  the below key added to the &quot;entity_type&quot; =&gt; &quot;folder&quot; item</p>
  <pre><code>&quot;children&quot; =&gt; [
    '#theme' =&gt; 'child_elements',
    '#child_elements' =&gt; [
      [
        'name' =&gt; 'text',
        &quot;id&quot; =&gt; &quot;333421&quot;,
        &quot;entity_type&quot; =&gt; &quot;node&quot;
      ],
      [
        'name' =&gt; 'folder',
        &quot;id&quot; =&gt; &quot;897622&quot;,
        &quot;entity_type&quot; =&gt; &quot;folder&quot;
      ],
      [
        'name' =&gt; 'text',
        &quot;id&quot; =&gt; &quot;342214&quot;,
        &quot;entity_type&quot; =&gt; &quot;node&quot;
      ],
    ],
  ],
  </code></pre>
  <p>and recursively it should keep on adding if &quot;entity_type&quot; =&gt; &quot;folder&quot;</p>
  <p>The final array should be</p>
  <pre><code>$result = [
        [
          &quot;name&quot; =&gt; &quot;text&quot;,
          &quot;id&quot; =&gt; &quot;928610&quot;,
          &quot;entity_type&quot; =&gt; &quot;node&quot;
        ],
        [
          &quot;name&quot; =&gt; &quot;folder&quot;,
          &quot;id&quot; =&gt; &quot;987620&quot;,
          &quot;entity_type&quot; =&gt; &quot;folder&quot;
          &quot;children&quot; =&gt; [
            '#theme' =&gt; 'child_elements',
            '#child_elements' =&gt; [
              [
                'name' =&gt; 'text',
                &quot;id&quot; =&gt; &quot;333421&quot;,
                &quot;entity_type&quot; =&gt; &quot;node&quot;
              ],
              [
                'name' =&gt; 'folder',
                &quot;id&quot; =&gt; &quot;897622&quot;,
                &quot;entity_type&quot; =&gt; &quot;folder&quot;
              ],
              [
                'name' =&gt; 'text',
                &quot;id&quot; =&gt; &quot;342214&quot;,
                &quot;entity_type&quot; =&gt; &quot;node&quot;
              ],
            ],
          ],
        ],
        [
          &quot;name&quot; =&gt; &quot;text&quot;,
          &quot;id&quot; =&gt; &quot;956720&quot;,
          &quot;entity_type&quot; =&gt; &quot;node&quot;
        ],
  ];
  </code></pre>
  <pre><code>public function buildTree($elements) {
  
        $branch = []; 
        $branch = ['#theme' =&gt; 'child_elements'];
       
        foreach ($elements as $key =&gt; $element) {
  
          foreach($element as $keys =&gt; $values){  
              
            if ($element['bundle'] == 'folder') {              
                $child = $this-&gt;loadElements($element['id']);              
                $branch['#child_elements'] = $child;
                $element[$key]['children'] = $branch['#child_elements'];
  
                $this-&gt;buildTree($child);
                
                array_push($branch, $element);
                
                    
             }else{
                
                $branch['#child_elements'][] = $element;
             }
          }
            
          
        }
  
      return $branch;
      }
  </code></pre>