Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 15th August 2010, 19:30
Rapid2214 Rapid2214 is offline
Senior Member
 
Join Date: Jun 2010
Posts: 105
Thanks: 3
Thanked 5 Times in 5 Posts
Unhappy 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
Reply With Quote
Sponsored Links
  #2  
Old 16th August 2010, 00:28
Rapid2214 Rapid2214 is offline
Senior Member
 
Join Date: Jun 2010
Posts: 105
Thanks: 3
Thanked 5 Times in 5 Posts
 
Lightbulb

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
Reply With Quote
The Following User Says Thank You to Rapid2214 For This Useful Post:
falko (16th August 2010)
Reply

Bookmarks

Tags
array compare, php, sort

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with services!! banzaiwebstudio.com Installation/Configuration 7 19th May 2010 22:13
Error when install ISPConfig 2 with components built from source X-admin Installation/Configuration 5 30th April 2010 14:22
Unable to install ISPConfig bdonecker Installation/Configuration 21 26th May 2009 09:20
ISP Config hesitation when opening web pages frankb Installation/Configuration 7 15th December 2008 14:06
Apache2 Freezes celtic Server Operation 31 28th May 2007 18:18


All times are GMT +2. The time now is 12:44.


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