I’m working with arrays of image filepaths. A typical array might have 5 image filepaths stored in it.
For each array, I want to pull out just the “best” photo to display as a thumbnail for the collection.
I find looping and arrays very confusing and after 4 hours of trying to figure out how to structure this, I’m at a loss.
Here are the rules I’m working with:
-
The very best photos have “-large” in their filepaths. Not all arrays will have images like this in them, but if they do, that’s always the photo I want to pluck out.
-
The next best photos are 260px wide. I can look this up with getimagesize. If I find one of these, I want to stop looking and use it.
-
The next best photos are 265 wide. If I find one I want to use it and stop looking.
-
The next best photos are 600px wide. Same deal.
-
Then 220px wide.
Do I need 5 separate for loops? 5 nested for-loops
Here’s what I’m trying:
if $image_array{
loop through $image_array looking for "-large"
if you find it, print it and break;
if you didn't find it, loop through $image_array looking for 260px wide.
if you find it, print it and break;
}
and so on….
But this doesn’t appear to be working.
I want to “search” my array for the best single image based on these criteria. If it can’t find the first type, then it looks for the second on so on. How’s that done?
1 Answer