How To Sort Multidimensional Array By Date In PHP

Sorting a multidimensional array by a date element is a common task in PHP, especially when dealing with data that includes timestamps. In this tutorial you will learn how to sort a multidimensional array by a date element in PHP using the built-in usort() function.

Dummy Multidimensional Array for Testing

Let’s start with an example of a multidimensional array that contains events with date information:

$events = [
    [
        'event' => 'Event A',
        'date' => '2023-10-15',
    ],
    [
        'event' => 'Event B',
        'date' => '2023-10-10',
    ],
    [
        'event' => 'Event C',
        'date' => '2023-10-20',
    ],
];

In this array, each event represents an associative array with two elements: ‘event’ denotes the event name and ‘date’ signifies the event date.

Sorting the Array Elements by Date using PHP usort()

To sort this array by the ‘date’ element, we’ll use the usort() function. This function takes the array to be sorted and a custom comparison function as arguments. The comparison function will determine the sorting order based on the ‘date’ element. Here’s the code to sort the array by date in ascending order:

usort($events, function($a, $b) {
    $dateA = strtotime($a['date']);
    $dateB = strtotime($b['date']);
    return $dateA - $dateB;
});

print_r($events);
Array
(
    [0] => Array
        (
            [event] => Event B
            [date] => 2023-10-10
        )

    [1] => Array
        (
            [event] => Event A
            [date] => 2023-10-15
        )

    [2] => Array
        (
            [event] => Event C
            [date] => 2023-10-20
        )

)

In this code:

  • We use the usort() function to sort the $events array.
  • The custom comparison function is defined as an anonymous function (lambda function). Inside the function, we use strtotime() to convert the ‘date’ elements of the two events into Unix timestamps.
  • We then subtract the timestamps to determine the sorting order.

Note: The usort() function will modify the original array.

Sorting the Array in Descending Order

If you want to sort the multidimensional array in descending order (from the most recent to the oldest), you can reverse the order of the timestamps in the comparison function:

usort($events, function($a, $b) {
    $dateA = strtotime($a['date']);
    $dateB = strtotime($b['date']);
    return $dateB - $dateA;
});

This code subtracts the timestamp of event B from the timestamp of event A, resulting in a descending order.

Printing the Sorted Array by Date Element

After sorting, you can print the sorted array to see the results:

foreach ($events as $event) {
    echo "{$event['event']} ({$event['date']})\n";
}

This loop will display the events in the sorted order.

# Ascending order
Event B (2023-10-10)
Event A (2023-10-15)
Event C (2023-10-20)

# Descending order
Event C (2023-10-20)
Event A (2023-10-15)
Event B (2023-10-10)

Complete PHP Code to Sort a Multidimensional Array by Date

<?php
$events = [
    [
        'event' => 'Event A',
        'date' => '2023-10-15',
    ],
    [
        'event' => 'Event B',
        'date' => '2023-10-10',
    ],
    [
        'event' => 'Event C',
        'date' => '2023-10-20',
    ],
];

usort($events, function ($a, $b) {
    $dateA = strtotime($a['date']);
    $dateB = strtotime($b['date']);
    return $dateA - $dateB;

    /**
     * For Descending Order:
     * return $dateB - $dateA;
     */
});

foreach ($events as $event) {
    echo "{$event['event']} ({$event['date']})\n";
}

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website. Privacy Policy