Jump to content

Sorting array values by month and year.


Saurdo

Recommended Posts

Hello! I have an array of values that I need to sort by month and year. This array may also contain values that are not months or a year.

 

I found the function "usort" but PHP's documentation isn't very good, I don't understand how to use it, and I'm not even sure that that's the function i need to use.

 

So, for example.

 

// This array
array('2004', 'March', 'January', '2006', 'chips', 'June', 'February', 'April', 'May', '1994', 'hotdogs');

// would turn into
array('1994', '2004', '2006', 'January', 'February', 'March', 'April', 'May',  'June', 'chips', 'hotdogs');

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/48167-sorting-array-values-by-month-and-year/
Share on other sites

did you try sort?

<?php
        $array = array('2004', 'March', '1979', 'April', '1995', 'February', '2000');

        echo "<pre>". print_r($array, true) ."</pre>\n";

        echo "Sorting...\n";

        sort($array);

        echo "<pre>". print_r($array, true) ."</pre>\n";
?>

 

you may also be curious with how php handles different types of data in a single array:

<?php
        $array = array('2004', 'March', 1979, 'April', '1995', 'February', 2000);

        echo "<pre>". print_r($array, true) ."</pre>\n";

        echo "Sorting...\n";

        sort($array);

        echo "<pre>". print_r($array, true) ."</pre>\n";
?>

 

and how it handles sorting associative arrays:

<?php
        $array = array('March' => '2004', 'March' => 2002, '1995' => 'January', 2010 => 'August');

        echo "<pre>". print_r($array, true) ."</pre>\n";

        echo "Sorting...\n";

        sort($array);

        echo "<pre>". print_r($array, true) ."</pre>\n";
?>

I don't want to sort alphabetically, which is what sort does, because then It'd return:

 

Array ( [0] => 1994 [1] => 2004 [2] => 2006 [3] => April [4] => February [5] => January [6] => June [7] => March [8] => May [9] => chips [10] => hotdogs ) 

 

I don't think the months go April, February, January, June, March, May. Unless those damn astronomers changed something again!  :-\

hmmm. i should have seen that by your example, but you were asking about usort which i don't think is necessary here.

<?php
        $array = array('2004', 'March', '1979', 'April', '1995', 'February', '2000');

        echo "<pre>". print_r($array, true) ."</pre>\n";

        echo "Sorting...\n";

        $new_array = array()
        foreach($array as $key => $val){ $new_array[] = strtotime($val); }

        sort($new_array);

        echo "<pre>". print_r($new_array, true) ."</pre>\n";
?>

 

that might work.

Umm, I turned that into this because the way you had it the script was just displaying the numbers.

 

<?php
        $array = array('2004', 'March', 'January', '2006', 'chips', 'June', 'February', 'April', 'May', '1994', 'hotdogs');

        echo "<pre>". print_r($array, true) ."</pre>\n";

        echo "Sorting...\n";

        $new_array = array();
        foreach($array as $key => $val){ $new_array[strtotime($val)] = $val; }

        ksort($new_array);

        echo "<pre>". print_r($new_array, true) ."</pre>\n";
?>

 

And that returns:

 

Array
(
    [0] => hotdogs
    [767048064] => 1994
    [1169452800] => January
    [1172131200] => February
    [1174546800] => March
    [1177225200] => April
    [1177297440] => 2004
    [1177297560] => 2006
    [1179817200] => May
    [1182495600] => June
)

 

I don't know what happened to the chips string... and I need to find a way to 2004 and 2006 display next to 1994. Perhaps in  a different array. Perhaps with a function that finds if there are alphabetical characters in the string. Thanks for the strtotime function though, didn't know about that one.

 

I just realized what happened to chips, it was replaced by hotdogs because both their keys are the same..

Okay!

 

I put the any value that's a month in a different array so it'd be easier to deal with.

 

<?php
        $array = array(2004, 'March', 'January', 2006, 'chips', 'June', 'February', 'April', 'May', 1994, 'hotdogs');

        echo "<pre>". print_r($array, true) ."</pre>\n";

        echo "Sorting...\n";

	function is_month($var){
		if(strtolower($var) == 'january' || strtolower($var) == 'february' || strtolower($var) == 'march' || strtolower($var) == 'april' || strtolower($var) == 'may' || strtolower($var) == 'june' || strtolower($var) == 'july' || strtolower($var) == 'august' || strtolower($var) == 'september' || strtolower($var) == 'october' || strtolower($var) == 'november' || strtolower($var) == 'december'){
		return true;}
		else{
		return false;}
		}
        $new_array = array();
        foreach($array as $key => $val){
	if(is_numeric($array[$key]) || !is_month($array[$key])){
	$new_array[] = $val;
	}
	else{
	$month_array[strtotime($val)] = $val; }

	}
	sort($new_array);
        ksort($month_array);


       foreach($new_array as $foo){
	echo "<pre>".$foo."</pre>";}

	foreach($month_array as $foo){
	echo "<pre>".$foo."</pre>";}

?>

 

Thanks for the help boo_lolly!

this is what i've got so far:

<?php
        $array = array('January', 2004, 'chips', 'March', 1998, 'August', 2003, 'hotdogs');
        
        $months = array('January', 'February', 'March', 'April',
                        'May', 'June', 'July', 'August', 'Sepember',
                        'October', 'November', 'December');

        $newMonthArray = array();
        $everythingElse = array();

        foreach($array as $val){
                foreach($months as $month){
                        if($val == $month){
                                $newMonthArray[] = strtotime($val);
                        }else{
                                $everythingElse[] = $val;
                        }
                }
        }
        
        $newMothArray = sort($newMonthArray);
        $everythingElse = sort($newMonthArray);
        
        echo "<pre>". print_r($newMonthArray, true) ."</pre>";
        echo "<pre>". print_r($everythingElse, true) ."</pre>";
?>

 

i'm sure there's a much better way of doing it, but try that for now while i work on something better.

I never liked nesting a foreach loop in a foreach loop because it seemed every time I did so it would end in a disaster. Which is what your script is doing, adding values over and over and over again.

 

I did however put the foreach in a function and it worked. I'd like to get rid of the need for a function altogether though because this script will be going inside a function.

 

<?php
        $array = array('2004', 'March', 'January', '2006', 'chips', 'June', 'February', 'April', 'May', '1994', 'hotdogs');

        echo "<pre>". print_r($array, true) ."</pre>\n";

        echo "Sorting...\n";

	function is_month($var){
		$months = array('january', 'february', 'march', 'april',
                        'may', 'june', 'july', 'august', 'sepember',
                        'october', 'november', 'december');
		foreach($months as $month){
			if($month == strtolower($var)){
				$p = 1;}
			}
			if($p == 1){return true;}
			else{return false;}
	}
        $new_array = array();
        foreach($array as $key => $val){
	if(is_numeric($array[$key]) || !is_month($array[$key])){
	$new_array[] = $val;
	}
	else{
	$month_array[strtotime($val)] = $val; }

	}
	sort($new_array);
        ksort($month_array);


       foreach($new_array as $foo){
	echo "<pre>".$foo."</pre>";}
	echo "Months<br>";
	foreach($month_array as $foo){
	echo "<pre>".$foo."</pre>";}

?>

Yeah, it  seems like if you could do a foreach loop in a function then you should be able to do it without it. I guess that's not the case though.

 

I'm looking forward to seeing your results because I haven't got a clue for a way around it. I've tried a for loop and even a do/while loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.