Home » Php » PHP Sort Array by date value

PHP Sort Array by date value

Posted by: admin July 12, 2020 Leave a comment


I have the following array format in my php code:

foreach ($events as $info) {
    $events_array[] = array(
        'title' => $info->Name,
        'date'  => $info->Date
function cb($a, $b) {
    return strtotime($a['date']) - strtotime($b['date']);
usort($events_array, 'cb');

Edit: The date values are in the format: YYYY-MM-DD

Actually, when I do print_r, I get

[title] => SimpleXMLElement Object ( ) [date] => SimpleXMLElement Object ( )
How to&Answers:

You have to create your own multi column sort function (because your array is 2-dimensional):

array_sort_by_column($events_array, 'date');


The sorting function:

function array_sort_by_column(&$array, $column, $direction = SORT_ASC) {
    $reference_array = array();

    foreach($array as $key => $row) {
        $reference_array[$key] = $row[$column];

    array_multisort($reference_array, $direction, $array);


You can use USort php function.

Give a look at this thread:

PHP Sort a multidimensional array by element containing date