title: Building a nested array with recursive function
body: <p>I got a JSON object like below which is pointing to a file structure. The objects that contain objects are folders, and others are files. And I'm downloading the contents in the URLs (The object can go much bigger or smaller.)</p>
  <pre><code>{
      &quot;foo&quot;: {
          &quot;bar&quot;: &quot;https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_2500kB.jpg&quot;,
          &quot;baz&quot;: &quot;https://sample-videos.com/img/Sample-jpg-image-100kb.jpg&quot;,
          &quot;caz&quot;: {
              &quot;zaz&quot;: &quot;https://sample-videos.com/img/Sample-jpg-image-100kb.jpg&quot;,
              &quot;kaz&quot;: {
                  &quot;haz&quot;: &quot;https://sample-videos.com/img/Sample-jpg-image-100kb.jpg&quot;
              }
          }
      }
  }
  </code></pre>
  <p>And I'm looping through that object and generating a unique id from each key-value pair.</p>
  <p>This is the recursive function that loops over the object and generates unique ids</p>
  <pre><code>&lt;?php
  
  function myRecursiveFunc($obj, &amp;$parentId = null, &amp;$result = [])
  {
      $result = [];
      $fileNames = array_keys($obj);
  
      foreach ($fileNames as $fileName) {
  
          if (is_array($obj[$fileName])) {
  
              $folderId = uniqueIdBuilder(); //Function that generate unique id
  
              buildResult($result, $parentId, $folderId, true); // The function to build $result object
  
              $parentId = $folderId;
  
              return myRecursiveFunc($obj[$fileName], $parentId, $result);
          } else {
  
              $url = $obj[$fileName];
  
              $fileId = uniqueIdBuilder($url);
  
              buildResult($result, $parentId, $fileId); // The function to build $result object
          
          }
      }
      return $result;
  }
  </code></pre>
  <p>Generating unique ids part is working. However, I want to build a result object to show generated unique ids with the exact file structure.</p>
  <p>So the result object should look like this:</p>
  <pre><code>[
    'fooId' =&gt; [
      'barId',
      'bazId',
      'cazId' =&gt; [
          &quot;zazId&quot;,
          &quot;kazId&quot; =&gt; [
              &quot;hazId&quot;
          ]
    ]
  ]
  </code></pre>
  <p>I have created the function that I will show below, but it does not work.</p>
  <pre><code>    function buildResult(&amp;$result, $parentId, $fileId, $isFolder = false)
      {
          if (count($result) &gt; 0) {
              foreach ($result as $key =&gt; $value) {
                  if (is_array($value)) {
  
                      if($key === $parentId &amp;&amp; !$isFolder)
                          return $result[$parentId][] = $fileId;
                          
                      if($key === $parentId &amp;&amp; $isFolder)
                          return $result[$parentId][] = [$fileId =&gt; []];
  
  
                      buildResult($value, $parentId, $fileId);
  
                  }
              }
          } else {
              $result[$parentId] = [];
          }
      }
  </code></pre>
  <p>The result doesn't have to look exactly like I mentioned above. The important thing for me is that this result object contains the generated unique ids.</p>
  <p>I'm open to any advice and help</p>
  <p>Edit:</p>
  <p>I'm getting this result from <code>buildResult()</code>:</p>
  <pre><code>[
     &quot;result&quot; =&gt; [ &quot;&quot; =&gt; []]
  ]
  
  </code></pre>