LTMH38 Posted November 1, 2016 Share Posted November 1, 2016 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); ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 1, 2016 Share Posted November 1, 2016 (edited) 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 November 1, 2016 by Psycho Quote Link to comment Share on other sites More sharing options...
Barand Posted November 1, 2016 Share Posted November 1, 2016 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(); } 1 Quote Link to comment Share on other sites More sharing options...
LTMH38 Posted November 2, 2016 Author Share Posted November 2, 2016 (edited) 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 November 2, 2016 by LTMH38 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.