0

I am trying to sort the arrays (each containing 2 date values) in a multidimensional array. I was able to find a useful function which solves the question for one element, however I was unable to modify it for two.

PHP Sort a multidimensional array by element containing date

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    return $t1 - $t2;
}    
usort($array, 'date_compare');

The problem at hand is sorting comments in which have a post time and an edit time. Essentially I want to sort them from newest to oldest (while keeping both values).

If this is not possible, let me know.

EDIT: Mockup

$array = array(
    [0] => array(
          [0] => "Aug:1:2012 12:00:pm", // post date
          [1] => "Aug:28:2012 12:00:pm"  // edit date
    ),
    [1] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:30:2012 12:00:pm"
    )
    [2] => array(
          [0] => "Aug:29:2012 12:00:pm",
          [1] => "Aug:1:2012 12:00:pm"
    )
};

Should output: $array[1] first (because it has the highest date out of keys 1 & 2) then $array[2], then $array[0].

$array = array(
    [0] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:30:2012 12:00:pm" // highest   
    ),
    [1] => array(
          [0] => "Aug:29:2012 12:00:pm", // next
          [1] => "Aug:1:2012 12:00:pm"
    )
    [2] => array(
          [0] => "Aug:1:2012 12:00:pm",
          [1] => "Aug:28:2012 12:00:pm" // lowest
    )
};
Community
  • 1
  • 1

2 Answers2

1

Your sort func needs to first work out which date is more recent - the post or edit date, then use that for the comparison.

function sort_arr($arr1, $arr2) {
    $this_posted = strtotime($arr1[0]);
    $this_edited = strtotime($arr1[1]);
    $comparison_posted = strtotime($arr2[0]);
    $comparison_edited = strtotime($arr2[1]);
    $this_date = $this_posted > $this_edited ? $this_posted : $this_edited;
    $comparison_date = $comparison_posted > $comparison_edited ? $comparison_posted : $comparison_edited;
    return $this_date > $comparison_date;
}

$arr = array(
    array("Aug:1:2009 12:00:pm", "Aug:2:2009 12:00:pm"),
    array("Aug:1:2011 12:00:pm", "Jul:21:2012 12:00:pm"),
    array("Aug:5:2011 12:00:pm", "Jan:21:2013 12:00:pm")
);

usort($arr, 'sort_arr');
Mitya
  • 33,629
  • 9
  • 60
  • 107
0

I'm not sure if understood correctly but you mean you want sort your array base on column1 ("post date") and if these values are equal order is determined by column2. So, what you need is just fix your comparing function:

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    if ($t1 != $t2) {
        return $t1 - $t2;
    }

    // if "post dates" are equal, compare "edit dates"
    $t1 = strtotime($a['datetime2']);
    $t2 = strtotime($b['datetime2']);

    return $t1 - $t2;
}    

edit:

ok, according to your comment, you just need to pick up max element from your array. So, this should work:

usort($array, function($a, $b) {
    $t1 = max(strtotime($a[0]), strtotime($a[1]));
    $t2 = max(strtotime($b[0]), strtotime($b[1]));

    return $t1 - $t2;
});
Cyprian
  • 11,174
  • 1
  • 48
  • 45
  • Not exactly, the post dates don't have to be equal. Think of the two date elements as a pool of dates, all I need is to sort this pool from newest to oldest. So the largest date value of the two dates would represent the array. –  Aug 30 '12 at 12:01
  • what do you mean - this doesn't work? (this sort in opposite direction comparing to your example, so if you want exactly the same, just change return to this: return $t2 - $t1;) – Cyprian Aug 30 '12 at 13:31
  • One more thing: my example works for php >= 5.3, so if you use lower version you need use normal function instead of anonymous one. But principle is the same – Cyprian Aug 30 '12 at 13:32
  • it is working now. After a lot of work looking for the warning offset notice, I noticed that dynamically corrected array had an empty array at the end thus causing an empty array without a key. Your code was extremely easy and useful, I can't thank you enough! –  Aug 30 '12 at 22:04