2
$array = array("2011-September_38","2011-June_4","2010-November_9","2011-November_29","2010-December_19");

i want to sort this array strings as following, it should sort the year first, then it should sort the month,

DESIRED OUTPUT

Array ( [0] => 2010-Marh_19 
        [1] => 2010-November_9 
        [2] => 2011-June_4 
        [3] => 2011-September_38
        [4] => 2011-November_29 )

I've tried something, could anyone bind my function to sort year then month http://codepad.org/skEiUkTC

1
  • btw "2010-Marh_19" is not in your array so it can't be in desired output :)
    – anubhava
    Dec 2, 2011 at 10:54

2 Answers 2

2

Try this code:

<?php
$array = array("2011-September_38","2011-June_4","2010-November_9","2011-November_29","2010-December_19");
function monthCompare($a, $b) {
   $da = strtotime(strstr($a, '_', true));
   $db = strtotime(strstr($b, '_', true));
   return $da > $db;
}
usort($array, "monthCompare");
print_r($array);
?>

OUTPUT

Array
(
    [0] => 2010-November_9
    [1] => 2010-December_19
    [2] => 2011-June_4
    [3] => 2011-September_38
    [4] => 2011-November_29
)
4
  • @balaji: Please see here: php.net/manual/en/function.strstr.php. As per this manual: 5.3.0 Added the optional parameter before_needle. which means you need to have at least php ver 5.3.0 to use this additional argument in strstr function. If you don't have php 5.3.0 or above pls let me know, I can provide you alternative solution.
    – anubhava
    Dec 2, 2011 at 10:57
  • I discovered the DateTime class when trying to solve this issue and i would recommend you take a look at it also, it gives you the ability to work with time in a very clean way and not by messing around with strstr inside strtotime :-) Dec 2, 2011 at 11:03
  • thanks anubhava, your concept is very simple, i was very confusing with my older code, now its working fine, i replaced your "strstr" with "substr" function, codepad.org/JELYk99G, i've edited your answer. now its ok.. Dec 2, 2011 at 11:07
  • Thanks that you liked it. However consider this solution for PHP ver < 5.3.0: codepad.org/tmwnyNtd
    – anubhava
    Dec 2, 2011 at 11:12
1

So a simple solution to this, is to turn each of the values into a objects with DateTime::createFromFormat, sort them, and then outputting the values again.

Before i saw this question i had never played with DateTime objects, but they are awesome and easy to work with, and createFromFormat makes perfectly sense.

$array = array("2011-September_30","2011-June_4","2010-November_9","2011-November_29","2010-December_19");

foreach($array as $item)
{
    $timestamps[] = DateTime::createFromFormat('Y-F_d',$item);
}

sort($timestamps);

foreach($timestamps as $timestamp)
{
    $newarray[] = $timestamp->format('Y-F_d');
}

will give you

Array
(
    [0] => 2010-November_09
    [1] => 2010-December_19
    [2] => 2011-June_04
    [3] => 2011-September_30
    [4] => 2011-November_29
)
4
  • As per the OP array element are not a date in the YYYY-Month_dd format. It is actually YYYY-Month_ID where ID can be any number like 68 or 95.
    – anubhava
    Dec 2, 2011 at 11:11
  • You must know the specific problem, as i was not able to read such from the question. Dec 2, 2011 at 11:14
  • Not your fault, actually OP earlier left a comment indicating that here: stackoverflow.com/questions/8351929/…
    – anubhava
    Dec 2, 2011 at 11:17
  • hi bro, the last digit is not date, its ID. thanks for your answer.nice concept. Dec 2, 2011 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.