Original title was *algorithm to list directories/files only from a particular object path (S3, Google Storage)*

<https://gist.github.com/9a353e1589ff3ce84c02>

Can anyone suggest an algorithm to list directories/files only within a particular object path? For example `ahostel.lt/img/` should list only directoies `languages and psd, and files background.png, [..]`. My algorithm is lengthy and uses three foreach cycles, which is bad for performance but maybe anyone have a better idea how to achieve it using regex or other alternatives.

My system is running on PHP but general logarithm would do just fine as long as it is possible to convert it to PHP.

In other words, I am looking for an algorithm like glob() which would work with an Array instead of File system.

Simplified directory list: <https://gist.github.com/d0c3fa12d4b894938ba5>