Create array from directory structure

Active3 hr before
Viewed126 times

4 Answers


I usually use this function to get folder structure,Connect and share knowledge within a single location that is structured and easy to search.,I need to iterate directory structure and push it to array with special structure. So I have next directories structure,Find centralized, trusted content and collaborate around the technologies you use most.

I usually use this function to get folder structure


function dirToArray($dir) {
    $contents = array();
    foreach (scandir($dir) as $node) {
        if ($node == '.' || $node == '..') continue;
        if (is_dir($dir . '/' . $node)) {
            $contents[$node] = dirToArray($dir . '/' . $node);
        } else {
            $contents[] = $node;
    return $contents;

$startpath = "path";

$r = dirToArray($startpath);

load more v

Web Development Forum , Programming Forum , htaccess querystring to directory 4 , Get Directory Listing 6

What I'd like to get is something like the following:

$paths = Array(
   file0.txt => file0.txt,
   dir1 => Array(
      filea.txt => filea.txt,
      fileb.txt => fileb.txt,
      dir1 => Array(
         filec.txt => filec.txt
   file1.txt => file1.txt,
   file2.txt => file2.txt
load more v

Returns an array of files and directories from the directory. , Returns an array of filenames on success, or false on failure. If directory is not a directory, then boolean false is returned, and an error of level E_WARNING is generated. ,scandir — List files and directories inside the specified path,readdir() - Read entry from directory handle

      [0] => .
      [1] => ..
      [2] => bar.php[3] => foo.txt[4] => somedir
      [0] => somedir[1] => foo.txt[2] => bar.php[3] => ..
      [4] => .

Consider a case, where we need to store the data of 5 students. We can store it by using the structure as given below. ,An array of structres in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structures.,Let's see an example of an array of structures that stores information of 5 students and prints it.,JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected] Duration: 1 week to 2 week

Enter the name, id, and marks of student 1 James 90 90
Enter the name, id, and marks of student 2 Adoms 90 90
Enter the name, id, and marks of student 3 Nick 90 90
Printing the details....
James 90 90.000000
Adoms 90 90.000000
Nick 90 90.000000
load more v

Other "directory-array" queries related to "Create array from directory structure"