sorting - How can I sort this array chronologically in php? -


i have array dates, looking this:

  $array = array(      "20140101000000" => "january 2014",      "20140201000000" => "february 2014",      "20140301000000" => "mars 2014",      "20130401000000" => "april 2013", // 2013 starts here      "20130501000000" => "may 2013",      "20130601000000" => "june 2013",      "20130701000000" => "july 2013",      "20130801000000" => "august 2013",      "20130901000000" => "september 2013",      "20131001000000" => "october 2013",      "20131101000000" => "november 2013",      "20131201000000" => "december 2013",   ); 

and wanted sorted chronologically, descending , have no idea on how achieve this. content of array 12 months current month , backwards in time. also, array received through api have no control on output.

basically want sorted in format:

mars 2014,  february 2014,  january 2014,  december 2013 april 2013 

first off have problem array, of items share keys. therefore once evaluated array going

$array = array(        "20140301000000" => "mars 2014",       "20130301000000" => "december 2013" ); 

which isn't going help. each item has have unique key.

if @ values of array, assuming:

$array = array(  "january 2014",  "february 2014",  "mars 2014",  "april 2013", // 2013 starts here  "may 2013",  "june 2013",  "july 2013",  "august 2013",  "september 2013",  "october 2013",  "november 2013",  "december 2013", ); 

you use user defined sort routine such as

function cmp($a, $b) {     if (strtotime($a) == strtotime($b)) {         return 0;     }     return (strtotime($a) > strtotime($b)) ? -1 : 1;  }  usort($array, "cmp"); 

see here http://uk1.php.net/manual/en/function.usort.php details on usort

alternatively if keys unique timestamp corresponding date use krsort.

krsort($array) 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -