PHP order array by date? [duplicate]
Asked Answered
C

4

45

Possible Duplicate:
PHP Sort a multidimensional array by element containing date

I have some data from XML or JSON in a PHP array that looks like this:

[0]= array(2) {
    ["title"]= string(38) "Another title"
    ["date"]= string(31) "Fri, 17 Jun 2011 08:55:57 +0200"
}
[1]= array(2) {
    ["title"]= string(38) "My title"
    ["date"]= string(31) "Mon, 16 Jun 2010 06:55:57 +0200"
}

What I want to do is order the two items by date.

  1. Is it possible to sort by date, when the sort value is inside every item?
  2. Do I need to convert the date format to timestamp?

What I don't want to do

I could use date and set it as the ID but that don't feel right, because two items can have the same date and then it would not be unique.

Costotomy answered 19/6, 2011 at 9:50 Comment(1)
Take a look at the first answer in this post: linkTimmons
L
116

You don't need to convert your dates to timestamps before the sorting, but it's a good idea though because it will take more time to sort without this step.

$data = array(
    array(
        "title" => "Another title",
        "date"  => "Fri, 17 Jun 2011 08:55:57 +0200"
    ),
    array(
        "title" => "My title",
        "date"  => "Mon, 16 Jun 2010 06:55:57 +0200"
    )
);

function sortFunction( $a, $b ) {
    return strtotime($a["date"]) - strtotime($b["date"]);
}
usort($data, "sortFunction");
var_dump($data);

Update

In newer PHP versions you can use arrow functions too. Here you can find a more concise version of the above:

usort($data, fn ($a, $b) => strtotime($a["date"]) - strtotime($b["date"]));
Loring answered 19/6, 2011 at 9:58 Comment(4)
Clean answer but this will lose values if two dates are equalWorden
That is not true, why would it? They would simply right after each other.Mesothorax
how would I do desc order?Cheapen
Just flip the arguments in the substraction, from $b["date"] the $a["value"].Mesothorax
S
43

Use usort:

usort($array, function($a1, $a2) {
   $v1 = strtotime($a1['date']);
   $v2 = strtotime($a2['date']);
   return $v1 - $v2; // $v2 - $v1 to reverse direction
});
Sunburn answered 19/6, 2011 at 9:56 Comment(1)
What about this version? usort($sortArray, function ($a, $b) { return strcmp($a['article_created_date'], $b['article_created_date']); });Rentschler
W
29

I recommend using DateTime objects instead of strings, because you cannot easily compare strings, which is required for sorting. You also get additional advantages for working with dates.

Once you have the DateTime objects, sorting is quite easy:

usort($array, function($a, $b) {
  return ($a['date'] < $b['date']) ? -1 : 1;
});
Walloon answered 19/6, 2011 at 9:58 Comment(0)
S
-2

He was considering having the date as a key, but worried that values will be written one above other, all I wanted to show (maybe not that obvious, that why I do edit) is that he can still have values intact, not written one above other, isn't this okay?!

<?php
 $data['may_1_2002']=
 Array(
 'title_id_32'=>'Good morning', 
 'title_id_21'=>'Blue sky',
 'title_id_3'=>'Summer',
 'date'=>'1 May 2002'
 );

 $data['may_2_2002']=
 Array(
 'title_id_34'=>'Leaves', 
 'title_id_20'=>'Old times',
  'date'=>'2 May   2002 '
 );


 echo '<pre>';
 print_r($data);
?>
Shiver answered 19/6, 2011 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.