Sort 2D array by date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4dplane
Forum Newbie
Posts: 6
Joined: Wed Nov 25, 2009 11:51 am

Sort 2D array by date

Post by 4dplane »

Hi, I have some code that sorts an array of dates that I first convert into YYYYDDMM format with sort($ISOSDates, SORT_NUMERIC). What I need to do is add a piece of data to each to each date array that is being sorted before it's sorted. So if I have an array sortedDateData that has dates added to it like this:

sortedDateData[$i] = array($year$monthNum$dayNum, $mediaId).

how can I sort the array by $year$monthNum$dayNum?

So the new sortedDateData[0][0] would have the first date
then sortedDateData[1][0] would have the next date

thanks,
4dplane
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Sort 2D array by date

Post by papa »

array_multisort

Should work.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Sort 2D array by date

Post by AbraCadaver »

I don't think you're going to get a proper sort using YYYYDDMM, but it depends upon what you want.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Sort 2D array by date

Post by daedalus__ »

why dont you convert them to unix timestamps then sort them as you add them to your list?
Post Reply