Jump to content

Converting arrays into a multi-dimensional array


LTMH38

Recommended Posts

I have a list of folder names like 1, 2, 3... Each folder has some .jpg files.
I get a list of all including files from this directories with two glob functions dynamically.
I store a list of directory paths and filenames into two separates arrays called $DIR[] and  $files[].
How can I convert this arrays to one multidimensional array and how can I get values in each dimension with this array?
 
<?php
        //FOLDERS
        //create array for each directory
        $pathDIR = "dirname/";
        $DIR = Array(
        );
        //store directory names into an array with foreach
        foreach (glob($pathDIR . '/[0-30]*', GLOB_ONLYDIR) as $dirname) {
            $DIR[] = $dirname;    
        }
        //display path directories
        for($a = 0; $a < count($DIR); $a++) {
            echo $DIR[$a] . "<br>";   
        }
        //FILES
        //create array for each directory
        $pathFILE = Array();
        //create array for files    
        $files = Array();            
        //loop start
        for($b = 1; $b <= count($DIR); $b++) {
            $pathFILE[$b] = "dirname/" . $b . "/"; 
            //store files in array with foreach
            foreach(glob($pathFILE[$b] . '*.jpg') as $filename) {
                $files[] = $filename;
            }
        }
        //display files
        for($c = 1; $c <= count($files); $c++) {
            echo $files[$c-1] . "<br>";
        }
        //convert arrays to multidimensional array 
        array_push ($DIR, $files);         
        ?>

 

Link to comment
Share on other sites

Well, what do you want the format of this multi-dimensional array to be? I typically use something like this

'folder1name' => array(
    'files' = array(list of files in folder1name),
    'folders' = array(
        'subfolder1name' =>array(
            'files' => array(list of files in folder1name),
            'folders'  = array(
                //Same format if there are more subfolders
            )
        ),
        'subfolder2name' =>array(
            'files' => array(list of files in folder1name),
            'folders'  = array(
                //Same format if there are more subfolders
            )
        )
    )
)
Edited by Psycho
Link to comment
Share on other sites

Use a recursive function EG

$dir='path/to/folder';

$results = [];
getFiles($dir, $results);
    // show results
echo '<pre>', print_r($results, 1), '</pre>';


function getFiles($dir, &$results)
{
    $d = dir($dir);
    while (false !== ($entry = $d->read())) {
       if ($entry=='.' || $entry=='..') continue;
       if (is_dir($dir.'/'.$entry)) {
           getFiles($dir."/$entry", $results['folders'][$entry]);
       }
       else $results['files'][] = $entry;
    }
    $d->close();
}

  • Like 1
Link to comment
Share on other sites

Thank Psycho and Barand.

I had tried with array_push:

array_push ($DIR, $file); 

to display array values I had tried with a pair of loops:

 for($d = 0; $d <= count($DIR); $d++) {
            for($e = 0; $e <= count($DIR[$d]); $e++) {
                echo $DIR[$d][$e] . "<br>";
            }
        }

Unfortunately array values are not properly show. Also, a notice indicate undefined offset .

Edited by LTMH38
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.