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??