Hi all. New both to the forum and to PHP so with 2 strikes already against me, I'll not try to add being terribly annoying as the third. That being said, I'm using a bit of code I wrote (albeit very poorly) to try to grab a folder of pictures as my directory, then sort, and eventually output those pictures. I haven't quite gotten to the output yet, because I'm having trouble with the sorting. The pictures are sorted by name, all in a standard format, for example, today the 8th of July would read 20100708-001. If multiple pictures were used, the -001 becomes -002, then -003, etc. I need the pictures in the most recent order first, which I could just typically use an arsort for - the catch is I want them in order for that day. For example, if I had pictures from the 2,4,5, and 8th of July, I would want them in 8,5,4,2 order, but if I had 3 pictures from the 8th, I would want them in 20100708-001, 20100708-002, 20100708-003, 20100705-001, 20100704-001, and finally 20100702-001. To accommodate this I tried using a multidimensional array with the first 8 characters being sorted by a standard sort, being somewhat modified by the second sort regarding characters 10-12. Didn't quite get it working, and as I said, the output isn't really coded yet. Anyway, hope I haven't been to wordy, and thanks for the help in advance. Here's what I have so far with obsolete bits commented out until I decide to do it for good or re-add them:
Code: Select all
<?php
$photo_Array = array();
if ($handle = opendir('/Users/web/Documents/Program_Photos')) {
//echo "Directory handle: $handle\n";
//echo "Files:\n";
/*Loops over the directory. */
while (false !== ($file = readdir($handle))) {
//echo "$file\n";
$first_date = substr($file, 0, 8);
$second_date = substr($file, 10, 12);
if (isset($prev_file) && $first_date == $prev_file) {
$photo_Array[$first_date][] = $file;
}
elseif ($first_date != $prev_file) {
//$prev_file = $first_date;
$photo_Array[$first_date][] = $file;
}
//$photo_Array[] = $file;
}
$prev_file = $first_date;
}
arsort($photo_Array);
// echo $photo_Array;
var_dump($photo_Array);
closedir($handle);
?>
The output I get with the vardump looks like the following.
array(5) { [20100702]=> array(2) { [0]=> string(16) "20100702-001.jpg" [1]=> string(16) "20100702-002.jpg" } [20100701]=> array(2) { [0]=> string(16) "20100701-001.jpg" [1]=> string(16) "20100701-002.jpg" } [20100604]=> array(1) { [0]=> string(16) "20100604-002.jpg" } [".."]=> array(1) { [0]=> string(2) ".." } ["."]=> array(1) { [0]=> string(1) "." } }