是否有一种简单的方法来订购此数组(日期将始终以正确的顺序显示)
0 => "Thursday,Sunday"
1 => "Tuesday"
2 => "Monday"
3 => "Tuesday,Thursday,Saturday"
4 => "Tuesday"
5 => "Monday,Wednesday"
6 => "Saturday,Sunday"
8 => "Wednesday"
9 => "Saturday"
10 => "Tuesday"
11 => "Monday,Sunday"
按此顺序(按星期几)
2 => "Monday"
5 => "Monday,Wednesday"
11 => "Monday,Sunday"
1 => "Tuesday"
4 => "Tuesday"
10 => "Tuesday"
3 => "Tuesday,Thursday,Saturday"
7 => "Tuesday,Friday"
8 => "Wednesday"
0 => "Thursday,Sunday"
9 => "Saturday"
6 => "Saturday,Sunday"
最后,我需要定义开始日期,但希望能够很容易地概括解决方案。
谢谢!
更新
下一个问题是如何重新排列
$day_map = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
从今天开始date('l')
。所以如果今天的Fridey我会得到
$rearranged_day_map = ['Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday'];
更新问题的答案(原问题的答案如下):
//days
$day_map = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
//order days to start from tomorrow
$today = date('N'); // get today's day of the week
$day_map_rearranged = array_merge(array_slice($day_map, $today),array_slice($day_map, 0, $today));
答案 0 :(得分:1)
正如其他人所建议的那样,你需要使用uasort
这个。我首先要做的是explode
字符串。然后为日序列(周一至周五)创建默认映射,并将其用于排序。
这是一个肮脏的想法:
// default order
$day_map = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
uasort($input_array, function($a, $b) use ($day_map) {
$c = explode(',', $a); $d = explode(',', $b);
for($i = 0, $len = count($c); $i < $len; $i++) {
$d1 = @array_search($c[$i], $day_map);
$d2 = @array_search($d[$i], $day_map);
if($d1 == $d2) {
continue; // skip, check the next column
}
return $d1 - $d2;
}
});
答案 1 :(得分:0)
试试这个。
$roster = array(
0 => "Thursday,Sunday",
1 => "Tuesday",
2 => "Monday",
3 => "Tuesday,Thursday,Saturday",
4 => "Tuesday",
5 => "Monday,Wednesday",
6 => "Saturday,Sunday",
8 => "Wednesday",
9 => "Saturday",
10 => "Tuesday",
11 => "Monday,Sunday");
$day = array(
0 => "Monday",
1 => "Tuesday",
2 => "Wednesday",
3 => "Thursday",
4 => "Friday",
5 => "Saturday",
6 => "Sunday");
$result = array();
$x=0;
foreach($day as $idx => $arrValue){
foreach($roster as $idxRoster => $arrRoster){
$list = explode(',', $arrRoster);
if($list[0] == $arrValue){
$result[$x]['id'] = $idxRoster;
$result[$x]['val'] = $arrRoster;
$x++;
}// end if
}// end foreach
}// end foreach
echo "<pre>";
print_r($result);