Now, to the point...
I've got a directory full of images, if the file name starts with "f!" then it's a thumbnail (and favourite), it it starts with "f" then it's just a favourite. Any other starting character(s) and it's just a normal image.
I want to output all these images, but I want the thumbnails (f!) to come out first, then the favourites, then the others. Also, I want each type of image(thumbs, faves and others) to be sorted by modification date seperately.
I couldn't even get usort to work with my own compare function when disregarding the date modification sorting (so the file names were just in a plain old array), so I quickly moved away from that method.
When I realised I needed date modification sorting too, I got all the filenames and dates in an associative array (filenames as keys, dates as values).
So I set about creating my own function to sort this associative array. The logical way to go about it was splitting the array into three 3 subparts, sorting them by date modified, and then concatenating them. After alot of messing around with array_push, and array_merge, I realised you can't actually concatenate arrays easily.
This is what I ended up with - it works, but surely it's not the best way to accomplish something like this?
Code: Select all
function imageSort(&$a)
{
//loop through each image
foreach($a as $k => $v)
{
//build the 3 sub parts
if(substr($k, 0, 2) == "f!")
$thu[$k] = $v;
else if($k{0} == "f")
$fav[$k] = $v;
else
$other[$k] = $v;
}
//sort the 3 subparts
asort($thu);
asort($fav);
asort($other);
//concatenate the arrays
foreach($fav as $k => $v)
$thu[$k] = $v;
foreach($other as $k => $v)
$thu[$k] = $v;
$a = $thu;
}