I am relatively new to PHP and programming in general and I am attempting to browse a directory and find all subdirectories, and all subdirectories of those subdirectories, and so on and so forth. I googled a bit and stumbled upon the concept of recursion, which I immediately knew was what I needed, because conceptually I already realized it but just didn't know whether or not it was possible. With my limited understanding of the PHP language structure (and general programming constructs for that matter) I'm hitting a bit of a wall here. I saw something about those iterators built into the language but I want to understand what's going into it and be able to put it together myself.
I've created a function that, as far as I can tell, should be able to take an array that has been pre-populated with the directories in the root folder (using a previous glob function) and then look into each of those and keep delving deeper until there are no new directories to be found...but that doesn't seem to be what's happening here. It's spitting out an error telling me that the '[]' operator is not supported for strings, but as far as I can tell '$arrayName[$key]' is an array object, not a string. Even if that particular problem is sorted out, could you experts tell me whether this will work, and if not what needs to be done?
$directory_list[0] = "directory1";
$directory_list[1] = "directory2";
$directory_list[2] = "directory3";
function iterate_unknown_directories($arrayName) {
foreach ($arrayName as $key => $arrayObject) {
$directories_check = glob($arrayObject."/*",GLOB_ONLYDIR);
foreach ($directories_check as $directory) {
if ($directory !== getcwd()) {
if ($directory !== $arrayObject) {
$arrayName[$key][] = $directory;
}
}
}
if (is_array($arrayName[$key])) {
iterate_unknown_directories($arrayName[$key]);
}
echo $arrayObject;
}
}
iterate_unknown_directories($directory_list);
I looked through the resources I was able to find, but none of them seemed to give me exactly what I need. Thank you so much for reading this far, and hopefully for lending a hand!