What would be the best way to sort this data via the date_submitted variable, then in another instance on the page by “likes,” and another instance by “dislikes?” I don’t want to sort the data through the query to save having to do the query 3 times on the page.
The variables MUST maintain their indexes too so that the subject matches the story, and so forth.
This is due to the improper use of the arrays. The data in each array is tightly related to each other.
I would suggest first using MySQL to sort the data before it’s fetched. That is something that databases are more effective at doing, than other available techniques.
If the data is then retrieved as an associated array, you can use custom sort methods to rearrange the data in a variety of acceptable manners.
You can pass to usort the name of a function that will specify how the ordering is to be decided. It won’t work on multiple arrays though, the data must be contained within the one (potentially associated) array.
What would be an effective way to put the arrays generated from the mysql results into one associative array, then sorting from there?
If I can’t find a way I’ll do the 3 queries on one page… but I’d rather not. Good to plan ahead in case my little project here becomes massive and I’m already optimized for the server load.
If you would like to sort by more than 1 column here is a function that uses array_multisort, and does not require you to write too much code, in just 1 line: