PHP - Sort Months into order

Discussion in 'Programming/Scripts' started by Rapid2214, Aug 15, 2010.

  1. Rapid2214

    Rapid2214 New Member

    Hello,
    I have an array which is produced by some other coding, essenitally it follows this santax:

    Code:
    array("September, "March", "December", "August", "January")
    What I want to be able to do is to order these, so it would be:

    Code:
    array("January", "March", "August", "September", "December")
    All i can think of doing is comparing it to the months in order, but can find a function to 'Sort By Array'.

    I've tried using array_multisort() but with no sucess.

    Any Ideas?

    Marty :cool:
     
  2. Rapid2214

    Rapid2214 New Member

    Fixed,
    I set the months using:
    Code:
    date(n)
    Which sets it to the month date 1 - January / 12 - December.
    Example array:
    Code:
    $retrived_months = array("6", "1", "12");
    I Then ordered that array using the command below:
    Code:
    asort($retrived_months); 
    This would give the months in numerical order.
    Code:
    $retrived_months = array("1", "6", "12");
    I then made a simple function to convert the numerical to the word:
    Code:
    foreach($retrived_months as $key => $value){getmonth($value)." ";}
    
    Which resulted in:
    Code:
    January June December
    Function
    Code:
    function getmonth($value)
    {
    switch($value){
    	case '1':
    	$month = "January";
    	break;
    	
    	case '2':
    	$month = "February";
    	break;
    	
    	case '3':
    	$month = "March";
    	break;
    	
    	case '4':
    	$month = "April";
    	break;
    	
    	case '5':
    	$month = "May";
    	break;
    	
    	case '6':
    	$month = "June";
    	break;
    	
    	case '7':
    	$month = "July";
    	break;
    	
    	case '8':
    	$month = "August";
    	break;
    	
    	case '9':
    	$month = "September";
    	break;
    	
    	case '10':
    	$month = "October";
    	break;
    	
    	case 11:
    	$month = "November";
    	break;
    	
    	case 12:
    	$month = "December";
    	break;
    	
    	}
    	return $month;
    }
    
    Which returned the Month 1 as January / Month 6 as June... Etc..
    Just as i needed! :)

    Is there a cleaner way of doing it?

    Hope this helps anyone else trying to achive a similar thing!

    Marty
     

Share This Page