HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   PHP - Sort Months into order (http://www.howtoforge.com/forums/showthread.php?t=47987)

Rapid2214 15th August 2010 19:30

PHP - Sort Months into order
 
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:

Rapid2214 16th August 2010 00:28

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


All times are GMT +2. The time now is 18:21.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.