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 8404349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:34:38+00:00 2026-06-09T22:34:38+00:00

So, I literally spent two days trying to get this into my head. I

  • 0

So, I literally spent two days trying to get this into my head. I want a function that creates a multidimensional array from a dirtree. I did it manually before and got this result:

Array ()

    [cats] = Array()
        [domestic] = Array ()
        [wild] = Array ()

    [dogs] = Array ()
        [big] = Array ()
        [small] = Array ()

    [emptyfolder]

By manually I mean doing something like this within my function: $result['level1']['level2'] = array(); This doesn’t work well because I don’t know the depth of the dirtree beforehand.

The function also uses another function GetSubdirectories($directory) which returns and array like this:

[0] = 'cats',
[1] = 'dogs'
[2] = 'emptyfolder'

So, here is the function in question:

<?php
public function GetStructure($directory, $node = 'content') //the root folder is content
    {
        if (!isset($structure))
            {
            $structure = array();
            }

        $subdirectories = $this->GetSubdirectories($directory);
        if (is_array($subdirectories))
            {
            foreach ($subdirectories as $subdirectory)
                {
                    //add new subarray
                    $structure[$node] = array();
                    //set the current path
                    $currentDirectory = $directory . "/" . $subdirectory;
                    //repeat
                    $this->GetStructure(); // <-- how to I call the function?
                }
            }
        return $structure;
        }
?>

From what I understand, the problem is “telling the function where to put the next arrays”. I just don’t get it and any help would be much appreciated!

SOLUTION

<?php
public function PopulateArrayWithDirtree($dir, &$array = false)
    {
    $subDirs = $this->GetSubdirectories($dir);
    if (is_array($subDirs))
        {
        for ($i = 0; $i < sizeof($subDirs); $i++)
            {
            $array[$subDirs[$i]] = array();
            $this->PopulateArrayWithDirtree(($dir . "/" . $subDirs[$i]), $array[$subDirs[$i]]); //repeat
            }
        }
    return true;
    }

public function Test()
    {
    $path = $this->contentpath;
    $structure = array();
    $this->PopulateArrayWithDirtree($path, $structure);
    print_r($structure);
    }
?>
  • 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-09T22:34:39+00:00Added an answer on June 9, 2026 at 10:34 pm

    It sounds like you want to access the next element?

    Don’t use a foreach, but a for loop:

    $keys = arraykeys($subdirectories);
    $length = sizeof($keys);
    for ($i = 0; $i < $length; $i++)
    {
        $structure[$node] = array();
        $currentDirectory = $directory . "/" . $subdirectory;
        $this->GetStructure($subdirectories[$i]);
    
    }
    

    however, if you mean set the result, then this may be it. I’m still confused:

    $keys = arraykeys($subdirectories);
    $length = sizeof($keys);
    for ($i = 0; $i < $length; $i++)
    {
        $currentDirectory = $directory . "/" . $subdirectory;
        $structure[$node] = $this->GetStructure($subdirectories[$i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have literally spent hours trying to fix this but cant! Like the title
I have literally spent days searching for a solution to this problem. PHP's mkdir()
I've spent literally weeks trying to get sections and rows to work in my
I've literally been trying to debug this for a full day and it's utterly
This is literally driving me nuts. I have 2 entities that use NSStrings as
I'm getting a very odd problem in Entity Framework query that I literally spent
Had a headace trying to get this to work, im trying to set up
I've literally spent the past half hour searching for the solution to this, and
I have literally spent hours with JQuery trying to extract a piece of text
I understand that: '\n' // literally the backslash character followed by the character for

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.