我每周都有一系列日子。我想通过开始日来重新排序数组。
$days_order = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
echo '<pre>';
print_r($days_order);
echo '</pre>';
输出
Array
(
[0] => Mon
[1] => Tue
[2] => Wed
[3] => Thu
[4] => Fri
[5] => Sat
[6] => Sun
)
问题:如何在设置开始日时重新排序数组?
注意:索引值很重要。
示例:
如果我将开始日设置为"Wed"
,那么数组会输出:
Array
(
[0] => Wed
[1] => Thu
[2] => Fri
[3] => Sat
[4] => Sun
[5] => Mon
[6] => Tue
)
答案 0 :(得分:0)
试试这个:
if (($i = array_search('Wed', $days_order)) !== false)
$result = array_merge(array_slice($days_order, $i), array_slice($days_order, 0, $i));
答案 1 :(得分:0)
尝试这样的事情:
$days_order = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
$split_by = array_search("Wed", $days_order);
if ($split_by) {
$first = array_slice($days_order, 0, $split_by);
$second = array_slice($days_order, $split_by);
$result = array_merge($second, $first);
}
print_r($result);
答案 2 :(得分:0)
无需预定义日期数组,只需循环日期()。
$start_date = 'Wed';
for ($i=0; $i<7; $i++) $arr[$i] = date('D', strtotime("Next $start_date +$i days"));