How can PHP functions like usort be utilized to sort arrays based on multiple columns, such as date and time together?

To sort arrays based on multiple columns, such as date and time together, we can use the usort function in PHP. This function allows us to define a custom comparison function that can compare multiple columns in the array elements. By comparing the date and time together in the custom function, we can achieve the desired sorting result.

// Sample array with date and time values
$data = [
    ['date' => '2022-01-15', 'time' => '08:30'],
    ['date' => '2022-01-15', 'time' => '10:00'],
    ['date' => '2022-01-14', 'time' => '12:00'],
];

// Custom comparison function for sorting by date and time
usort($data, function($a, $b) {
    $dateComparison = strcmp($a['date'], $b['date']);
    if ($dateComparison == 0) {
        return strcmp($a['time'], $b['time']);
    }
    return $dateComparison;
});

// Output sorted array
print_r($data);