-1

I have an array that looks something like this:

  Array
(
    [0] => Array
        (
            [id] => 4
            [date] => 15.12.2014
            [archived] => 0
        )

    [1] => Array
        (
            [id] => 3
            [date] => 19.12.2014
            [archived] => 0
        )

    [2] => Array
        (
            [id] => 6
            [date] => 15.11.2014
            [archived] => 0
        )
)

What I would like to do is sort the items into high-to-low order in the first dimension based on the date value in the second dimension. I can use strtotime() on the [date] field and produce a unix timestamp (please note, these dates are in Australian format and not US. The server produces the correct timestamp).

I'm aware that I can use arsort() to arrange this array, but I'm not sure how to do it based on the value of a second dimension array key.

I need the array to look like this:

  Array
(

    [0] => Array
        (
            [id] => 3
            [date] => 19.12.2014
            [archived] => 0
        )
    [1] => Array
        (
            [id] => 4
            [date] => 15.12.2014
            [archived] => 0
        )
    [2] => Array
        (
            [id] => 6
            [date] => 15.11.2014
            [archived] => 0
        )
)

How can I best achieve this in PHP?

I've tried various arrangements of the following to no avail:

arsort($items, strtotime(['date']))
ES-AKF
  • 103
  • 1
  • 2
  • 5

2 Answers2

1

You should use usort

In your case:

usort($items, function($a, $b) {
    return strtotime($a['date']) - strtotime($b['date']);
});
krynio
  • 2,442
  • 26
  • 30
0

Suppose that your $array is declared that way:

$my_array = array(
              array(
                'id'=>4,
                'date'=>'19.12.2014',
                'archived'=>0),
              array(
                 'id'=>3,
                 'date'=>'15.12.2014',
                 'archived'=>0),
              array(
                 'id'=>6,
                 'date'=>'15.11.2014',
                 'archived'=>0));

You can use usort function with a closure function as follows

usort($my_array, function($a, $b) {
    return strtotime($a['date']) - strtotime($b['date']);
});

usort will sort your array "in place", means that no copy of array will be returned but original array is sorted.
Moreover, I've used strtotime to make order between dates as a starting dates are represent as strings and not in "real date" format.

Moreover keep in mind that

Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.

Execution test

Array
(
    [0] => Array
        (
            [id] => 6
            [date] => 15.11.2014
            [archived] => 0
        )

    [1] => Array
        (
            [id] => 4
            [date] => 15.12.2014
            [archived] => 0
        )

    [2] => Array
        (
            [id] => 3
            [date] => 19.12.2014
            [archived] => 0
        )
)
DonCallisto
  • 29,419
  • 9
  • 72
  • 100