I have an array in the form:
$sortme = array(
'Monday 10:00',
'Friday 12:00',
'Tuesday 14:00',
'Monday 08:00',
'Wednesday 11:00',
);
The number of elements is unknown (so I couldn't use the uksort approach with a reference array for the order of weekdays as Mark suggested here). I want the result to be sorted chronologically and always starting with Monday like this:
$sorted = array(
'Monday 08:00',
'Monday 10:00',
'Tuesday 14:00',
'Wednesday 11:00',
'Friday 12:00',
);
Is there an elegant way to do it without resorting to transform the array to a two-dimensional one and iterating? This would have been my only idea so far.
You can use usort
function in PHP, which will do sorting using user-specified comparison function. The code below is probably not the most efficient, but it'll produce the correct result.
$sortme = array(
'Monday 10:00',
'Friday 12:00',
'Tuesday 14:00',
'Monday 08:00',
'Wednesday 11:00',
);
usort($sortme, function($a, $b) { return strcmp(date('N H:i', strtotime($a)), date('N H:i', strtotime($b))); });
print_r($sortme);
I was trying a one-liner with array_multisort
and array_map
or call_user_func_array
but my brain is fried. Try this:
foreach($sortme as $val) {
$sorter[] = date('N H:i', strtotime($val));
}
array_multisort($sorter, SORT_ASC, $sortme);
EDIT: One-long-liner:
array_multisort(array_map(function($v){return date('N H:i',strtotime($v));}, $sortme), SORT_ASC, $sortme);
array_multisort(
array_map(
function($v){
return date('N H:i',strtotime($v));
},
$sortme),
SORT_ASC, $sortme);
$sortme = array(
'Monday 10:00',
'Friday 12:00',
'Tuesday 14:00',
'Monday 08:00',
'Wednesday 11:00',
);
$sorted = array();
// Add the day of week in the begining of the string
for($i = 0; $i < count($sortme); $i++){
$day = explode(' ', $sortme[$i]);
@$dayNumber = date('w', strtotime($day[0]));
array_push($sorted, $dayNumber . $day[0] . ' '. $day[1]);
}
// Sort the array
asort($sorted);
// Remove the day of week
for($i = 0; $i < count($sorted); $i++){
$sorted[$i] = substr($sorted[$i], 1);
}
var_dump($sorted);
You just have to convert the array items into something that is sortable:
function sortTimes(array $dates) {
$base = strtotime('next monday 00:00');
usort($dates, function($a, $b) use ($base) {
return strtotime($a, $base) - strtotime($b, $base);
});
return $dates;
}