-1

I have an multi-dimensional array, which has the index ['dates']. Inside ['dates'] there is another index, and inside there is a ['date_time'] index with some data.

Presently the ordering of this part of the array is from oldest to newest date/time. I wish to reverse this so that the first element is the most recent data.

Here is a subset of the data:

array(4) {
  [0]=>array(3) {
    ["id"]=>string(4) "1279"
    ["name"]=>string(13) "account_name"
    ["dates"]=>array(7) {
      [0]=>array(3) {
        ["date_time"]=>string(19) "2014-11-14 09:30:03"
        ["follower_count"]=>string(4) "1567"
        ["gains_losses"]=>string(4) "1567"
      }
      [1]=>array(3) {
        ["date_time"]=>string(19) "2014-11-14 16:30:35"
        ["follower_count"]=>string(4) "1566"
        ["gains_losses"]=>string(2) "-1"
      }...

Here is the full array

Francesca
  • 26,842
  • 28
  • 90
  • 153
  • possible duplicate of [Sort Multi-dimensional Array by Value](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) – RichardBernards Nov 19 '14 at 10:26
  • Look at http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value on how to do this. Since your dates are in the YYYY-MM-DD format, you can just sort them by value. – RichardBernards Nov 19 '14 at 10:27

1 Answers1

1

You might use Date obj with format instead of strtotime to avoid suprises.

// Comparison function
function cmp($compare, $with) {
    $a = $compare['date_time'];
    $b = $with['date_time'];
    if (strtotime($a) == strtotime($b)) {
        return 0;
    }
    return ($a > $b) ? -1 : 1;
}

foreach($your_arr as &$item){
    uasort($item['dates'], 'cmp');
}
Jarek.D
  • 1,274
  • 1
  • 8
  • 18
  • I've just added this function to my code, but don't seem to be seeing any change. I am not seeing any errors either. Where should the function be called? – Francesca Nov 19 '14 at 11:03
  • I've added & operator in order to foreach to modify $your_array in place. Try again, sorry, J – Jarek.D Nov 19 '14 at 11:31