Page 1 of 1

Sorting array

Posted: Fri Aug 13, 2010 4:42 pm
by db579
I have an array called $picture_array. I want to sort the files in it by date created and then return the first 8 files. I've tried to map the date created to the file and loop through the array like so:

Code: Select all

$Array = glob("gallery/*");

foreach ($Array as $album){
$picture_array = glob("$album/*_th.jpg");

foreach($picture_array as $time_pic){
$picture_array = array(filemtime($time_pic) => $picture_array);

$i=1;
while($i<=8)
  {
  echo $picture_array[$i];
  $i++;
  }
}}
Unfortunately it doesn't return anything. Can anyone point me in the right direction? Thanks very much

Re: Sorting array

Posted: Sat Aug 14, 2010 12:28 am
by arrielmabale
found this code from http://www.webcheatsheet.com/PHP/workin ... tories.php, this would be better.

Code: Select all

<?php
// open the current directory
$dhandle = opendir('.');
// define an array to hold the files
$files = array();

if ($dhandle) {
   // loop through all of the files
   while (false !== ($fname = readdir($dhandle))) {
      // if the file is not this file, and does not start with a '.' or '..',
      // then store it for later display
      if (($fname != '.') && ($fname != '..') &&
          ($fname != basename($_SERVER['PHP_SELF']))) {
          // store the filename
          $files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
      }
   }
   // close the directory
   closedir($dhandle);
}

echo "<select name=\"file\">\n";
// Now loop through the files, echoing out a new select option for each one
foreach( $files as $fname )
{
   echo "<option>{$fname}</option>\n";
}
echo "</select>\n";
?>
after collecting and iterating through the file

Code: Select all

<?php
$arrayFiles = array();
foreach (new DirectoryIterator('.') as $file) {
   if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) {
	 $arrayFiles[$file->getFilename()] = $file->getMTime();
   }
}
sort($arrayFiles);
//now get only the 0-7 index values
?>
display the 8 latest that you need ^_^
God Speed

Re: Sorting array

Posted: Sat Aug 14, 2010 4:41 pm
by db579
Sorry why would that be better? Was trying to do it so that I could loop through a set of subfolders in a specific folder and filter by file extension.

I think my $picture_array contains the files I need and their corresponding date created as a key. The while loop I have returns the first value 8 times. I'm not sure how to get it to select the next date created value and then return the corresponding file. Can anyone show me how to do that? Thanks very much.

This is my current code:

Code: Select all

$i=1;

$Albums = glob("gallery/*");

foreach ($Albums as $album){
$Pictures = glob("$album/*_th.jpg");

foreach($Pictures as $picture){
$time = filemtime($picture);
$picture_array = array($time => $picture);
while($i<=8)
  {
  echo $picture_array[$time];
  $i++;
  }
}			
}

Re: Sorting array

Posted: Sun Aug 15, 2010 4:58 pm
by db579
I think the easiest way of doing this would be to create/modify my array so that it acts like a table with 3 columns two containing file and date created (fixed relative to each other) and a keyid going from 0 to whatever that is not fixed relative to the other two by which the other two could be selected. Have tried googling but kind find how to do this. Would be grateful if anyone could give me a pointer. Thanks