-1

I am wanting to sort an array in PHP by date and then output it in JSON. My array contains a lot of 'posts'. Here is how it currently appears when I json_encode the current array and echo it out as json. I think I may have to use usort() but I'm not sure how to do this.

I have tried this suggestion PHP Sort a multidimensional array by element containing date but it doesn't work for me. Please can anyone help. Thanks

{
"post": [
    {
        "title": "Title 1",
        "date": {
            "strdate": "2013-04-09",
            "dayname": "Tue"
        }
    },
    {
        "title": "Title 2",
        "date": {
            "strdate": "2013-04-08",
            "dayname": "Mon"
        }
    }
]

}

My array in PHP looks like this:

    <?php 

    Array
    (
        ["post"] => Array
            (
                [0] => Array
                    (
                        ["title"] =>   Title 
                        ["date"] => Array
                            (
                                ["strdate"] => 2013-04-09
                                ["month"] => 04
                                ["dayname"] => Tue
                            )

                    )

                [1] => Array
                    (
                        ["title"] =>   Title2 
                        ["date"] => Array
                            (
                                ["strdate"] => 2013-04-08
                                ["dayname"] => Mon
                            )

                    )

            )

    )

     ?>
Community
  • 1
  • 1
Aaron Lumsden
  • 396
  • 1
  • 4
  • 19

2 Answers2

2
usort($array['post'], function ($a, $b) {
    return strtotime($b['date']['strdate']) - strtotime($a['date']['strdate']);
});
Explosion Pills
  • 176,581
  • 46
  • 285
  • 363
1

Try this from here,

function mysort($a, $b)
{
   return(strtotime($b['timestamp']) - strtotime($a['timestamp']));
}


// Convert first date & time to UNIX timestamp

$t1 = mktime(2, 30, 0, 7, 1, 2011);
$t2 = mktime(3, 30, 0, 7, 1, 2011);
$t3 = mktime(4, 30, 0, 7, 1, 2011);


// Store data to a Multidimensional Arrays
$testArray = array(
    "testing3" => array("timestamp" => $t1, 
                        "name" =>"testing3"),

    "testing" => array("timestamp" => $t2, 
                       "name" =>"testing"),

    "testing2" => array("timestamp" => $t3, 
                        "name" =>"testing2")
    );



usort($testArray, 'mysort');

echo "<pre>";
print_r($testArray);

echo "

";

foreach($testArray AS $row){

 $date = $row['timestamp'];
 echo date("d/m/y : H:i:s a",$date)."
";
}

See also:

Community
  • 1
  • 1
Tony Stark
  • 7,818
  • 8
  • 41
  • 63
  • Copied from http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date, http://stackoverflow.com/a/96870 and http://chicalicdan.blogspot.fi/2011/02/sort-time-date-inside-multidimensional.html – JJJ Apr 23 '13 at 09:57
  • @Juhana, i have search on google & find the best answer so i post the answer. It's wrong bro. Can i just only post link as answers. – Tony Stark Apr 23 '13 at 10:20
  • I've edited the answer as an example. If another Stackoverflow post answers the question, don't copy it as a new answer but flag this question to be closed as a duplicate, or give the link in a comment. – JJJ Apr 23 '13 at 10:32
  • @Juhana Thanks bro i will make sure in future. – Tony Stark Apr 23 '13 at 10:35