1,105,242 Community Members

Selecting Random Images from Multiple Folders

Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
0
 

My brain is not functioning! I am trying to select a number of random images (lets say 3) from a selection of folders within one other folder and I am either over complicating the code or am missing something as I can't quite get it to work.

What I have so far is

$imgDirectory = 'images/portfolio/';
$results = scandir($imgDirectory);
$images = array();
foreach ($results as $result) {
    if ($result != '.' && $result != '..') {
        $imgDir = 'images/portfolio/'.$result.'/';
        $images[] = glob($imgDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    }
}
$randomImages = array_rand($images, 3);

foreach ($randomImages as $value) {
    echo $value.'<br>'; // this is just set up for testing at the moment
}

My code is getting all of the sub-folders as expected and then is also getting all the images in each folder. However when I try to echo those images, all I am getting is the array index number. If I dump the array, I get

Array ( 
    [0] => Array ( ) 
    [1] => Array ( 
        [0] => images/portfolio/kitchens/0_64446400_1339145292.jpg 
        [1] => images/portfolio/kitchens/0_98163100_1339145225.jpg
    ) 
    [2] => Array ( 
        [0] => images/portfolio/patios/0_08724600_1339141112.jpg 
        [1] => images/portfolio/patios/0_16298600_1339141074.jpg 
    ) 
    [3] => Array ( 
        [0] => images/portfolio/stonework/0_13904600_1339140361.jpg 
        [1] => images/portfolio/stonework/0_25698100_1339141591.jpg 
    ) 
    [4] => Array ( 
        [0] => images/portfolio/stoves-fireplaces/wood-burning-stove.jpg 
        [1] => images/portfolio/stoves-fireplaces/empty-fireplace.jpg 
    ) 
)

There are more images but I didn't think you needed to see the full list. As you can see I have a multi-dimensional array with a random empty array at index 0 and I cannot access the image files in the sub-array. What am I missing??

Member Avatar
blocblue
Practically a Posting Shark
828 posts since Jan 2008
Reputation Points: 238 [?]
Q&As Helped to Solve: 162 [?]
Skill Endorsements: 16 [?]
Featured
 
0
 

On line 7, you want to merge the arrays, not add one as an item to the other. This assumes that the array keys are numeric.

$images = array_merge($images + glob($imgDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE));
Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
0
 

Thank you, that makes sense and I have implemented the code which is now working better, in that I have an array as I expect it to be.

The problem now arising is that I am only getting 10 array items printed out when I should have 24.
The current code

$imgDirectory = 'images/portfolio/';
$folders = scandir($imgDirectory);
$images = array();
foreach ($folders as $folder) {
    if ($folder != '.' && $folder != '..') {
        $imgDir = 'images/portfolio/'.$folder.'/';
        $images = array_merge($images + glob($imgDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE));
    }
}
print_r($images);

Gives me

Array ( 
    [0] => images/portfolio/fireplaces/empty-fireplace.jpg 
    [1] => images/portfolio/fireplaces/fireplace-work-in-progress-1.jpg 
    [2] => images/portfolio/fireplaces/fireplace-work-in-progress-2.jpg 
    [3] => images/portfolio/fireplaces/fireplace-work-in-progress-3.jpg 
    [4] => images/portfolio/fireplaces/stove-installation-2.jpg 
    [5] => images/portfolio/fireplaces/wood-burning-stove.jpg 
    [6] => images/portfolio/fireplaces/stove-installation.png 
    [7] => images/portfolio/fireplaces/stoves-main-image.png 
    [8] => images/portfolio/stonework/0_82615200_1339141214.jpg 
    [9] => images/portfolio/stonework/house-stonework.jpg 
)

As you can see there are only 2 image folders being added to the array when there should be 4 (if I echo out the $folder in the foreach I get all 4 folders) and also in the images/portfolio/stonework folder there are actually 10 images in the folder but only 2 have been added to the array.

I can see no reason why the number of array items is 'limited' and why the other 2 folders aren't being added to the array.

Any ideas?

Member Avatar
blocblue
Practically a Posting Shark
828 posts since Jan 2008
Reputation Points: 238 [?]
Q&As Helped to Solve: 162 [?]
Skill Endorsements: 16 [?]
Featured
 
1
 

Sorry, it should have been a comma to separate the arguments in array_merge not a plus.

It'll be limited because adding two arrays with a + overwrites duplicate indexes. array_merge doesn't overwrite duplicate numeric keys.

Member Avatar
simplypixie
Practically a Master Poster
673 posts since Oct 2010
Reputation Points: 123 [?]
Q&As Helped to Solve: 122 [?]
Skill Endorsements: 6 [?]
 
0
 

You can tell I don't work with arrays very much (well not like this anyway) :) That's got it sorted, thank you again.

Question Answered as of 1 Year Ago by blocblue
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article