-1

I have an array of date like

    a[0]=>2013-10-05
    a[1]=>2013-10-25
    a[2]=>2013-10-15
    a[3]=>2013-10-28

I want to sort in ascending order. How can i sort this?

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
Bibek Jana
  • 104
  • 8
  • 2
    What you tried so far? – Alma Do Oct 29 '13 at 06:16
  • PHP has many functions for sorting arrays, see http://php.net/manual/en/array.sorting.php Have you tried them? For example what did asort() return and why that did not match your expectations? – Harri Oct 29 '13 at 06:18

2 Answers2

2

Try this;

$orderByDate = $my2 = array();
foreach($data as $key=>$row)
{
    $my2 = explode('-',$row[1]);
    $my_date2 = $my2[1].'-'.$my2[0].'-'.$my2[2];        
    $orderByDate[$key] = strtotime($my_date2);  
}    
array_multisort($orderByDate, SORT_ASC, $data);
BKM
  • 6,949
  • 7
  • 30
  • 45
1

Try the default sort function as stated here: Sort by Date

Or use a custom sort function like suggested here: Custom Search Function

function sortFunction( $a, $b ) 
{
    //$a and $b are two values from your array
    //Return a value > 0 then $a is greater than $b
    return strtotime($a) - strtotime($b);
}
usort($data, "sortFunction");
Community
  • 1
  • 1
SpazzMarticus
  • 1,218
  • 1
  • 20
  • 40