Q: Foreach in multidimensional array doesnt work I have two methods for looking into directories and making a tree.
public function getFolders($path)
{
    $folders = \Storage::disk('public')->directories($path);

    return count($folders) > 0 ? $folders : [];
}

public function getTree($path = '')
{
    if (count($tree = $this->getFolders($path)) === 0) {
        return ['nema'];
    }

    $arr = [];
    foreach ($tree as $value) {
        $folder = str_replace($path, '', $value);
        if (count(explode('/', $folder)) === 1) {
            if (count($this->getFolders($value)) > 0) {
                $arr[$folder] = $this->getTree($value . '/');
            } else {
                $arr[$folder] = $folder;
            }
        }
    }

    return $arr;
}

When I dd($arr) I get:
array:1 [▼
  "files" => array:2 [▼
    422 => array:1 [▶]
    "shares" => array:2 [▼
      "sasic" => array:2 [▼
        "daasdasd" => "daasdasd"
        "thumbs" => "thumbs"
      ]
      "thumbs" => "thumbs"
    ]
  ]
]

But when I try this:
$test = 0;
foreach($arr as $folder) 
{  
    if(is_array($folder)){
        $test = $test + 1;
    }
}

return $test; // returns 0 like there is zero arrays

The strange thing is that if I use echo in a foreach, it doesn't go from top to bottom but instead in some mixed order.
Any ideas?
I am trying to make the menu tree from only directories within some folder in Laravel storage public disk.
Update: I added $test in the loop here by writing this part of the code. Didn't copy that so, that's not answering my question as to why the values are not in an array.

A: You need to use "recursion" in this case.
function CountFolders($folder_array,$amount)
{
    foreach($folder_array as $folder)
    {
        if(is_array($folder))
        {
            $amount = $amount + 1;
            $amount=CountFolders($folder,$amount);

        }
    }
    return $amount;
}

echo'Total folders: '.CountFolders($arr,0);

This function checks and if found an array calls SELF on it and repeats until all arrays counted.
This way you don't need to know the max "deepness" of array.

A: You should use PHP's OOP approach using iterator classes:
class DirectoryArrayTreeIterator extends RecursiveDirectoryIterator
{
  function current()
  {
    return ($this->isDir()) ? iterator_to_array($this->getChildren()) : $this->getFilename();
  }
}

$iter = new DirectoryArrayTreeIterator('/path/to/folder', DirectoryArrayTreeIterator::SKIP_DOTS | DirectoryArrayTreeIterator::KEY_AS_FILENAME);
var_dump(iterator_to_array($iter));


A: In case you have nothing...
You can use same recursion, but don't need to store anything to array.
<?php

You need a function and I suggest to use URL and AMOUNT parameters
function CountFolders($url,$amount)
{

Function gets list of files and folders in current folder
$urlm=scandir($url);    

Loops trough them
foreach($urlm as $folder)
{

Checks if it found a directory or a file. 
If it is a directory and not a link to go back ("." and ".."), counts it to amount of directories.
if((is_dir($url.'/'.$folder))&&($folder!='.')&&($folder!='..'))
{
    $amount = $amount + 1;

Optional, to show a list of directories found.
Here you can store directory name or URL to array if needed.
//echo $url.'/'.$folder.' - <strong>DIR</strong><hr>';

If it is a directory, check it for another directories in it
        $amount=CountFolders($url.'/'.$folder,$amount);
    }
  }

Function returns amount of directories in a directory.
  return $amount;
}

Lastly show the result
echo'Total directories: '.CountFolders('../TestFolder',0);
?>

And all code in one place:
<?php

function CountFolders($url,$amount)
{
    $urlm=scandir($url);    
    foreach($urlm as $folder)
    {
        if((is_dir($url.'/'.$folder))&&($folder!='.')&&($folder!='..'))
        {
            $amount = $amount + 1;

            echo $url.'/'.$folder.' - <strong>DIR</strong><hr>';

            $amount=CountFolders($url.'/'.$folder,$amount);
        }
    }
    return $amount;
}

echo'Total folders: '.CountFolders('../TestFolder',0);

?>

