PHP Sort Array by date value
27,699
You have to create your own multi column sort function (because your array is 2-dimensional):
array_sort_by_column($events_array, 'date');
var_dump($events_array);
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);
}
Author by
Dave
Updated on October 15, 2020Comments
-
Dave over 3 years
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 ( )