Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:10:06+00:00 2026-06-18T01:10:06+00:00

I am relatively new to PHP and programming in general and I am attempting

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:10:07+00:00Added an answer on June 18, 2026 at 1:10 am

    I’m fairly certain you are over complicating the problem. Try taking a look at this:

    function iterate_unknown_directories($dirs)
    {
        foreach ($dirs as $dir)
        {
            $subDirs = glob($dir . "/*", GLOB_ONLYDIR);
    
            /* print out all the new directories*/
            foreach ($subDirs as $subDir)
                echo $subDir . "<br>";
    
            // recursively search the new directories
            iterate_unknown_directories($subDirs);
        }
    }
    
    $directories = array(
        'test_dir'
    );
    
    iterate_unknown_directories($directories);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to php development but not to web development in general. I
I'm relatively new to PHP and have exhausted the internet trying to find an
I'm relatively new to php and I've been trying all day long to get
I'm relatively new to client-side programming coming from the PHP/MySQL environment. I understand the
I'm relatively new to coding in general, first year CS student and all of
I am relatively new to programming with PHP. I have created a simple Javascript
I'm relatively new to web programming. New to Javascript, PHP, Ajax, etc. I have
i am relatively new to php and i've encountered a very strange problem. let
I am relatively new to PHP, but experienced Java programmer in complex enterprise environments
This may be a silly question, but as someone relatively new to PHP, I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.