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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:32:27+00:00 2026-06-04T21:32:27+00:00

I have data contained in an array which is like so, $file[‘info’][‘files’] = array(

  • 0

I have data contained in an array which is like so,

$file['info']['files'] = array(
    [0] => array(
         'length' => (int),
         'path' => array (
              [0] => 'file.txt',
         ),
    ),
    [1] => array(
         'length' => (int),
         'path' => array (
              [0] => 'directory one',
              [1] => 'file2.txt',
         ),
    ),
    [2] => array(
         'length' => (int),
         'path' => array (
              [0] => 'directory one',
              [1] => 'directory two',
              [2] => 'file3.txt',
         ),
    ),
);

The $file['info']['files'] array can contain any number of elements. The path array contained in each $file['info']['files'] array is where I am having trouble.

It contains information about a file structure. If just 1 element exists then it is a file. If more than one element exists then each element starting from the top is a parent folder of the next element and the last element is the file in the last folder. Taking the example above would be a file structure of

FILE file1.txt
FOLDER directory one
     FILE file2.txt
     FOLDER directory two
          FILE {file3.txt}

I would like to extract this data into my own array structure which is to be as follows,

 $sortedFiles = array(
     'file1.txt' => (int),
     'directory one' => array(
         'file2.txt' => (int),
         'directory two' => array(
              'file3.txt' => (int),
         ),
     ),
 );

I have this code so far,

foreach($file['info']['files'] as $file) {
    // LENGTH AND PATH ARE SET
    if(isset($file['length'], $file['path'])) {
        // GET COUNT OF FILE PATH ARRAY
        $count = count($file['path']);
        // SINGLE FILE
        if($count == 1) {
            $sortedFiles[$file['path'][0]] = $file['length'];
        // FILES IN DIRECTORY
        } else {
            // BUILD ARRAY STRUCTURE FOR DIRECTORIES
        }
    }
}

I am having trouble when it comes to adding directories to the array. I could do it manually and only go so many directories down each time checking if the array for the directory exists and if not create it, and if it does exist then add to it. I tried this with the code below but it only goes one directory deep (the code went where it says // BUILD ARRAY STRUCTURE above).

// FOLDER NOT SET
if(!isset($files[$file['path'][0]])) {
    $sortedFiles[$file['path'][0]] = array($file['path'][1] => $file['length'],);
// FOLDER SET
} else {
    $sortedFiles[$file['path'][0]][$file['path'][1]] = $file['length'];
}

How can I dynamically create arrays for each directory that exists and add the information that is needed bearing in mind that the directory structure could be any amount of levels deep?

Thanks for taking the time to read my rather long question and I appreciate any help that anyone gives to me.

  • 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-04T21:32:29+00:00Added an answer on June 4, 2026 at 9:32 pm

    You will need to call your function recursively, as in the example below:

    function get_contents_dir( $dir )
    {
        $names = array();
    
        if ( is_dir($dir) && is_readable($dir) )
        {
                foreach ( scandir($dir) as $file )
                {
                        if ( is_dir($dir."/".$file) && is_readable($dir."/".$file) )
                        {
                                $names[] = get_contents_dir($dir."/".$file);
                        }
    
                        if ( is_file($dir."/".$file) && is_readable($dir."/".$file) )
                        {
                                $names[] = $dir."/".$file;
                        }
                }
        }
    
        return $names;
    }
    

    This function first opens the set $dir folder and scans the list of files, adding each found file to the array which is, after scanning the folder, returned as the return value of the function.

    The twist comes in when an entry of the scandir() result (list of files and folders in the folder) is actually a folder. If that happens, the function is called from it’s internals, recursively (see the line $names[] = get_contents_dir($dir."/".$file); calling the function from within the function) and the subfolder will be indexed too. Rinse and repeat, until all subfolders are indexed.

    If you call the function and let it execute, an array will be returned. Each key of the array will be an entry. If it was a file, the value linked to the key is the name of the file, if it was a folder, the value will be another array nested into the previous one.

    Here is an example dump taken of the returned array:

    array (
      0 => './libmysqlclient.so.16.0.0',
      1 => './libmysqlclient_r.so.16.0.0',
      2 => 
      array (
        0 => './libs/libboost_thread-mt.a',
        1 => './libs/libboost_thread-mt.so.1.38.0',
        2 => './libs/libmysql.dll',
        3 => './libs/libmysqlclient16_5.1.41-3ubuntu12_i386.deb',
      ),
      3 => 
      array (
        0 => './radio_sneaker/cl_auto.lua',
        1 => './radio_sneaker/sh_auto.lua',
        2 => './radio_sneaker/sh_coms.lua',
        3 => './radio_sneaker/sh_info.lua',
        4 => './radio_sneaker/sv_auto.lua',
        5 => './radio_sneaker/sv_hooks.lua',
      ),
      4 => './sv_auto.lua',
    )
    

    Compare this output against the tree command ran on the same folder:

    |   libmysqlclient.so.16.0.0
    |   libmysqlclient_r.so.16.0.0
    |   sv_auto.lua
    |   
    +---libs
    |       libboost_thread-mt.a
    |       libboost_thread-mt.so.1.38.0
    |       libmysql.dll
    |       libmysqlclient16_5.1.41-3ubuntu12_i386.deb
    |       
    \---radio_sneaker
            cl_auto.lua
            sh_auto.lua
            sh_coms.lua
            sh_info.lua
            sv_auto.lua
            sv_hooks.lua
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .txt file with product data, which I want to read in
I have some xml data contained in three files (Database.xml, Participants.xml, and ConditionTokens.xml). I
I'd like to output some data to a file. For example assume I have
I have a multi-dimensional array which contains ten thousands of data. A lot... The
I have a raw pointer which points to an array of data. I would
I have a xml file which contains data I want to insert into a
I have a variable that contains an array from form data, seen below: $option1
In an embedded Jetty container I have a servlet which pushes data to the
For an XML file I am creating I have data that contains a bullet
HI all, I created a .net object (ex: A ) which contain data have

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.