mrpatg
mrpatg

Reputation: 10117

grouping and handling parts of loop results in php

I have a massive directory full of images. The images are structured between 1 and 3 directories deep. Im using php5 directory iteration to return the paths of each of these files, which im going to build an xml file for afterward.

What im trying to do, is once i reach a directory with a group of images, id like to store those images in an array that i can then loop through and handle.

Im using the code below, which gets me a read out of all images, with their full directories;

$it = new RecursiveDirectoryIterator("basedir/");
foreach(new RecursiveIteratorIterator($it) as $file) {

$filearray = explode("/", $file);

unset($output);

    foreach ($filearray AS $key=>$value){


        if(strstr($value, 'basedir')){

            $output .= "";  //ignore the top level directory

        }else if(!strstr($value, '.jpg') && !strstr($value, '.JPG')){

            $output .= "$value - "; //if its a directory name and not an image, add the path

        }else{

            $output .= "$value <br>"; //if its a picture, add the filename and linebreak

        }
    }

echo $output;

This results in an output similar to below;

accessories - J.L. Johnson Bridals 2011 - 123.jpg
accessories - J.L. Johnson Bridals 2011 - 234.jpg
accessories - J.L. Johnson Bridals 2011 - 345.jpg
accessories - J.L. Johnson Bridals 2011 - 456.jpg 
bridal - Allure Bridals - Women - 123.jpg
bridal - Allure Bridals - Women - 234.jpg
bridal - Allure Bridals - Women - 345.jpg 
prom - MacDuggal Spring 2011 - MD prom - 123.jpg
prom - MacDuggal Spring 2011 - MD prom - 234.jpg
prom - MacDuggal Spring 2011 - MD prom - 345.jpg
prom - MacDuggal Spring 2011 - MD prom - 456.jpg 

What im looking to achieve, is grouping the images in a single directory, perhaps into an array, that i can then loop through. Something like this;

accessories
    J.L. Johnson Bridals 2011
        123.jpg
        234.jpg
        345.jpg
        456.jpg 

bridal
    Allure Bridals
        Women
            123.jpg
            234.jpg
            345.jpg 
prom
    MacDuggal Spring 2011
        MD prom
            123.jpg
            234.jpg
            345.jpg
            456.jpg 

Upvotes: 1

Views: 79

Answers (1)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99939

If I understand well, you want to read a directory recursively, and get the result in a tree of arrays ?

Try this:

function read_recursive($dir) {
        if (is_dir($dir)) {
                $list = scandir($dir);
                $result = array();
                foreach($list as $key) {
                        if ($key == '.' || $key == '..') continue;
                        $result["$dir/$key"] = read_recursive("$dir/$key");
                }
                return $result;
        } else {
                return $dir;
        }
}
print_r(read_recursive('/some/dir'));

This will return an array in which each element is either an array (is the element is a directory) or a filename (the element is a filename). Each array element follows the same rules.

All pathes are absolute. Here is a version which returns relative pathes:

function read_recursive($dir) {
        if (is_dir($dir)) {
                $list = scandir($dir);
                $result = array();
                foreach($list as $key) {
                        if ($key == '.' || $key == '..') continue;
                        $result[$key] = read_recursive("$dir/$key");
                }
                return $result;
        } else {
                return basename($dir);
        }
}

The output will be something like this:

Array(
    accessories => array(
        J.L. Johnson Bridals 2011 => array(
            123.jpg => 123.jpg
            234.jpg => 234.jpg
            ...
        )
    )
    bridal => array(
....

Upvotes: 2

Related Questions