Sort a Multidimensional Array by Date Element in PHP

In PHP, you can sort a multidimensional array by a specific element, such as a date, using various approaches. Let's explore three different methods ?

Using array_multisort()

The array_multisort() function sorts multiple arrays or multidimensional arrays based on one or more columns ?

<?php
$multiArray = array(
    array('date' => '2023-06-01', 'name' => 'John'),
    array('date' => '2023-05-15', 'name' => 'Alice'),
    array('date' => '2023-06-10', 'name' => 'Bob')
);

// Extract the 'date' column into a separate array
$dates = array_column($multiArray, 'date');

// Sort the multidimensional array based on the 'date' column
array_multisort($dates, SORT_ASC, $multiArray);

// Output the sorted array
print_r($multiArray);
?>
Array
(
    [0] => Array
        (
            [date] => 2023-05-15
            [name] => Alice
        )

    [1] => Array
        (
            [date] => 2023-06-01
            [name] => John
        )

    [2] => Array
        (
            [date] => 2023-06-10
            [name] => Bob
        )

)

Using usort() with Custom Comparison Function

You can define a custom comparison function for more complex sorting logic ?

<?php
$multiArray = array(
    array('date' => '2023-06-01', 'name' => 'John'),
    array('date' => '2023-05-15', 'name' => 'Alice'),
    array('date' => '2023-06-10', 'name' => 'Bob')
);

// Define a custom comparison function
function compareDates($a, $b) {
    $dateA = strtotime($a['date']);
    $dateB = strtotime($b['date']);
    
    if ($dateA == $dateB) {
        return 0;
    }
    return ($dateA < $dateB) ? -1 : 1;
}

// Sort the array using the custom comparison function
usort($multiArray, 'compareDates');

// Output the sorted array
print_r($multiArray);
?>
Array
(
    [0] => Array
        (
            [date] => 2023-05-15
            [name] => Alice
        )

    [1] => Array
        (
            [date] => 2023-06-01
            [name] => John
        )

    [2] => Array
        (
            [date] => 2023-06-10
            [name] => Bob
        )

)

Using array_multisort() with Timestamp Conversion

Convert dates to timestamps for more accurate sorting, especially with different date formats ?

<?php
$multiArray = array(
    array('date' => '2023-06-01', 'name' => 'John'),
    array('date' => '2023-05-15', 'name' => 'Alice'),
    array('date' => '2023-06-10', 'name' => 'Bob')
);

// Create an array to hold the timestamp values
$timestamps = array();
foreach ($multiArray as $key => $row) {
    $timestamps[$key] = strtotime($row['date']);
}

// Sort using timestamps for accurate date comparison
array_multisort($timestamps, SORT_ASC, $multiArray);

// Output the sorted array
print_r($multiArray);
?>
Array
(
    [0] => Array
        (
            [date] => 2023-05-15
            [name] => Alice
        )

    [1] => Array
        (
            [date] => 2023-06-01
            [name] => John
        )

    [2] => Array
        (
            [date] => 2023-06-10
            [name] => Bob
        )

)

Comparison

Method Best For Performance Complexity
array_multisort() Simple date strings High Low
usort() Complex comparison logic Medium Medium
Timestamp conversion Mixed date formats High Medium

Conclusion

Use array_multisort() for simple date sorting, usort() for complex comparison logic, and timestamp conversion for handling various date formats. All methods effectively sort multidimensional arrays by date elements.

Updated on: 2026-03-15T10:38:57+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements