Roc*_*ket 0 php sorting date multidimensional-array
I have an array as follows:
$array = array (
array( 'Day' => 'Monday',
'Start' => '0830',
'End' => '1730'),
array( 'Day' => 'Monday',
'Start' =>'1730',
'End' => '2130'),
array( 'Day' => 'Tuesday',
'Start' => '0600',
'End' => '1100'),
array( 'Day' => 'Tuesday',
'Start' => '0600',
'End' => '0900'),
);
Run Code Online (Sandbox Code Playgroud)
I'm trying to work out how to sort this so the results are sorted first by Day and then by the earliest Start, then End.
Using the above array, the results would be:
Monday 0830-1730
Monday 1730-2130
Tuesday 0600-0900
Tuesday 0600-1100
Run Code Online (Sandbox Code Playgroud)
Can any advise the best way to do this. Thanks
因此,如@Daniel所写,仅使用usort函数,您还需要检查如何将Day表示为int-为此,请检查日期函数手册-以我的解决方案N
格式使用,因此,星期一-1 ...星期日-7逻辑用于排序只是一个https://en.wikipedia.org/wiki/Polynomial
<?php
$array = array (
array( 'Day' => 'Monday',
'Start' => '0830',
'End' => '1730'),
array( 'Day' => 'Monday',
'Start' =>'1730',
'End' => '2130'),
array( 'Day' => 'Tuesday',
'Start' => '0600',
'End' => '1100'),
array( 'Day' => 'Tuesday',
'Start' => '0600',
'End' => '0900'),
);
usort($array, function($a, $b) {
return (date("N", strtotime($a['Day'])) <=> date("N", strtotime($b['Day']))) * 100 +
($a['Start'] <=> $b['Start']) * 10 +
($a['End'] <=> $b['End']);
});
var_dump($array);
Run Code Online (Sandbox Code Playgroud)