There is a problem in this code snippet:
foreach (glob($dir . '/*.{jpg,png,gif}', GLOB_BRACE) as $images) { $files[] = $images; }
The $dir variable was used to iterate over the array returned by the first call to glob() :
foreach (glob(...) as $dir) { ... }
Its value after the first foreach is the last value that was assigned to it in the foreach .
At the end, $data['albums'] contains all the directory names and $data['files'] contains the file names inside the last directory specified in $data['albums'] .
I cannot use array_values ββfor $ data ['files'], as I did for $ data ['albums'], since it gives a warning about converting a string to a string.
array_values() does not create anything new for $dirname and $files . If they are created (!), These two variables contain values ββindexed by consecutive numbers, starting at 0 ; this is exactly what array_values() returns.
The purpose of the code you posted is not very clear to me. I assume that you want to display a list of albums and how many images each album contains.
Here's how I do it:
public function viewphotoalbumsAction() { $identity = $this->identity(); // Always initialize the arrays before putting values into them. // Without this, if the first glob() returns an empty array, the outer // foreach loop never runs and both $dirname and $files end up being undefined // and this produces trouble in the code that uses these variables later. $dirname = array(); // This will be a two dimensional array. It is indexed by directory // names and contains the lists of files for each directory. $files = array(); // The outer loop enumerates the albums foreach (glob(getcwd() . '/public/images/profile/' . $identity . '/albums/*', GLOB_ONLYDIR) as $dir) { // The directory name is the album name $albumName = basename($dir); // Put the album name in $dirname[] // This is not really needed as we also have the album name // as key in $files but can be useful if you want to keep more // information about each album $dirname[] = $albumName; // Initialize the list of images of this album $files[$albumName] = array(); // The inner loop enumerates the images of this directory foreach (glob($dir . '/*.{jpg,png,gif}', GLOB_BRACE) as $images) { $files[] = $images; } } // Prepare the data for display $data = array( 'albums' => $dirname, 'files' => $files, ); return new ViewModel($data); }
View (HTML shell is ignored, this is normal as it is):
<?php foreach ($this->files as $albumName => $listFiles): ?> <p> <?php echo $albumName; ?> - Number of images: <?php echo count($listFiles); ?> </p> <?php endforeach; ?>
As a remark, the inner foreach is not even needed, because all it does is copy one value from the array returned by glob() one by one to the new array.
It is faster and easier to read and understand, simply storing the value returned by glob() in $files[$albumName] . Since the values ββstored in $dirname are never used, this variable can be completely omitted and the function becomes shorter (comments are omitted):
public function viewphotoalbumsAction() { $identity = $this->identity(); $files = array(); foreach (glob(getcwd().'/public/images/profile/'.$identity.'/albums/*', GLOB_ONLYDIR) as $dir) { $albumName = basename($dir); $files[$albumName] = glob($dir . '/*.{jpg,png,gif}', GLOB_BRACE); } return new ViewModel(array('files' => $files)); }