Good day! Help a beginner please! I receive a list of folders through Yandex.Disk api:

$result = json_decode($result,true); foreach ($result['_embedded']['items'] as $file) { $extpath = $file['name']; 

Then for each folder you need to get a list of files, I do another foreach inside this:

 $result3 = json_decode($result3,true); foreach ($result3['_embedded']['items'] as $file3) { $extpath2 = $file3['name']; 

And at the end of all cycles if I do return $extpath; then only the folder list is displayed. And if return $extpath; , then only a list of files in all folders.

Attention, question: How can I get separately and a list of folders and a list of files? To make it like this:

Folder1 >> File1, File2
Folder2 >> File1, File2, File3

Thank!

  • Please provide the full code with two nested loops - Mikhail Rebrov

1 answer 1

You need to create an associative array: Example method:

 <?php $folder = array(); $result = json_decode($result,true); foreach ($result['_embedded']['items'] as $file) { $extpath = $file['name']; $result3 = json_decode($result3,true); foreach ($result3['_embedded']['items'] as $file3) { $extpath2 = $file3['name']; array_push($folder[$extpath],$extpath2); } } print_r($folder); ?> 

Since you showed code snippets (it's not clear where you got $result3 ), I wrote a general logic about what you need to do to get the result.

 Folder1 >> File1, File2 Folder2 >> File1, File2, File3 

PS In the future, try to fully show the code.