9

I have a array of month names currently ordered like ("April", "August", "February") etc. I would like to re order this list so that it is in a normal month order such as ("January", "February", "March")

This array is getting populated from a SHOW TABLES sql query and unfortunately the SHOW TABLES does not have a ORDER BY parameter so I think my best bet is to add them to an array and reorder the array to get what I am looking for.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Ron
  • 277
  • 1
  • 2
  • 13

5 Answers5

19

Convert the months to a numerical value. Then order your array numerically using sort()

You can use this question: convert month from name to number

@Matthew's answer seems to work well:

$date = date_parse('July');;
echo $date["month"];

Working solution

$months = array("April", "August", "February");

usort($months, "compare_months");
var_dump($months);

function compare_months($a, $b) {
    $monthA = date_parse($a);
    $monthB = date_parse($b);

    return $monthA["month"] - $monthB["month"];
}
Community
  • 1
  • 1
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
3

This is a slightly optimized version (without date parsing) ^^

$foobar_months = array( 'april','februari', 'march', 'may', 'june', 'januari', 'august', 'october', 'july', 'november', 'december', 'september' );
usort( $foobar_months, "sortMonths" );
var_dump( $foobar_months );

function sortMonths ( $a, $b ) {
  $months = array( 'januari', 'februari', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december' );
  if ( array_search( $a, $months) == array_search( $b, $months) ) return 0;
  return array_search( $a, $months) > array_search( $b, $months) ? 1 : -1;
}
Johan
  • 1,958
  • 11
  • 20
2
$input = array('May', 'December', 'March', 'July');
$output = array();

foreach($input as $month) {
    $m = date_parse($month);
    $output[$m['month']] = $month;
}
ksort($output);

var_dump($output);

outputs

array
  3 => string 'March' (length=5)
  5 => string 'May' (length=3)
  7 => string 'July' (length=4)
  12 => string 'December' (length=8)
billyonecan
  • 20,090
  • 8
  • 42
  • 64
0

It is not possible to sort the array as a standalone array, for the system has no way of knowing that January comes first, followed by February, etc. You can define a hash initially, like

a = {'January':0,'February':1,...'December':11}

Then you can sort your array in this manner

array_to_be_sorted = sorted(array_to_be_sorted, key = lambda(x): a[x])
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
0

You can follow below step

  1. Get the output of SHOW TABLES , which will be un-orders list of months ("April", "August", "February")
  2. pass this to switch case as below
    $output_show_tables = ("April", "August", "February") $order_month_list = array(); foreach($output_show_tables as $month){

switch($month) case 'january': $order_month_list[1] = january; break; $order_month_list[2] = February; break; .. ... $order_month_list[12] = December; break;

}

  1. now you get order list of months
Ganesh Bora
  • 1,133
  • 9
  • 17