I have the following array that I want to sort, how would I approach this as it only has one key / column?
Short brief: I have movies that are saved in a custom post type. some have time allocated using a date picker and saved in a meta field. The reason I check if they are not empty is for the same reason.
This is what my script looks for like (no filter as yet):
Would it be easier to sort if the date/time fields were in a different format? That is, if the date was YYYYMMDD would a standard sort routine simplify the job?
Actually, I think it’s just like @Dormilich said above, use usort or, if you need to maintain the relationship between the key and the contents, uasort:
uasort($yourrray, function($a, $b) {
if ($a['show_date_and_time'] > $b['show_date_and_time']) return -1;
if ($a['show_date_and_time'] < $b['show_date_and_time']) return 1;
return 0;
});
I can’t remember which “direction” that will sort in, if it’s the wrong way around for you then either swap the < and > signs, or swap the -1 and 1 return values.
All that’s happening is uasort is calling the function I’ve defined below it to perform the comparison to see whether array elements need to be moved. You could retain your original date / time format, but that would need you to extract the year, month and date within the function to do the comparison. Swapping them to YYYYMMDD can just use ASCII values to get them in order, so much simpler to code.
I think that will do it, it’s similar to another question someone asked a while back on a similar topic.