从特定开始日期开始对天数数组进行排序

Sort array of days from a specific start date

我有这一系列随机排列的天数:

$jour_planning[] = "friday";
$jour_planning[] = "wednesday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";
$jour_planning[] = "thursday";
$jour_planning[] = "sunday";
$jour_planning[] = "saturday";

如果我们今天是 "tuesday",我想要这个新数组:

$jour_planning[] = "wednesday";
$jour_planning[] = "thursday";
$jour_planning[] = "friday";
$jour_planning[] = "saturday";
$jour_planning[] = "sunday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";

如何使用 usort() 做到这一点?

此致, 薇安妮

    while (end($jour_planning) !== "tuesday") {
        array_unshift($jour_planning, array_pop($jour_planning));
    }

我会先对数组进行排序(并避免使用法语和英语命名 :))。

然后,循环排序的数组并存储到 2 个单独的数组中,在预期的前几天(包括这一天)和之后的几天。

最后,合并两个数组。

肯定有更好的方法。

$jour_planning[] = "friday";
$jour_planning[] = "wednesday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";
$jour_planning[] = "thursday";
$jour_planning[] = "sunday";
$jour_planning[] = "saturday";

$sorted_days_planning = array(1 => null, 2 => null, 3 => null, 4 => null, 5 => null, 6 => null, 7 => null);

foreach ($jour_planning as $value)
{
    $day_of_week = date('N', strtotime($value));
    $sorted_days_planning[$day_of_week] = $value;
}

$final_days_planning = array();

$day_to_start = "tuesday";
$day_found = false;
$temp_array = array();
foreach ($sorted_days_planning as $value)
{
    if (!$day_found)
    {
        $temp_array[] = $value;
        if ($day_to_start == $value)
            $day_found = true;
    }
    else
        $final_days_planning[] = $value;
}

$final_days_planning = array_merge($final_days_planning, $temp_array);

var_dump($final_days_planning);

输出:

array(6) {
    [0]=> string(9) "wednesday"
    [1]=> string(8) "thursday"
    [2]=> string(6) "friday"
    [3]=> string(8) "saturday"
    [4]=> string(6) "sunday"
    [5]=> string(6) "monday"
    [6]=> string(7) "tuesday"
}

因为你的数组是随机排序的,所以没有其他方法可以正确排序,然后手动排序(你不能按数字或字母顺序排序数组 - 它可以通过 datetime() 函数,见 答案),所以:

/*
$jour_planning = [];

$jour_planning[] = "friday";
$jour_planning[] = "wednesday";
$jour_planning[] = "monday";
$jour_planning[] = "tuesday";
$jour_planning[] = "thursday";
$jour_planning[] = "sunday";
$jour_planning[] = "saturday";
*/

$days = [
    0 => 'monday',
    1 => 'tuesday',
    2 => 'wednesday',
    3 => 'thursday',
    4 => 'friday',
    5 => 'saturday',
    6 => 'sunday'
];

$today = array_search(strtolower(date('l')), $days);
$tomorrow = ($today === 6) ? 0 : $today + 1;
$reordered = array_merge(array_slice($days, $tomorrow), array_slice($days, 0, $tomorrow));

print_r($reordered);

首先你必须搜索当天,然后将这一天的索引加1,你就有了这一天,那就是明天。现在,当你有了这个索引时,你只需创建 2 个数组:第一个从明天开始,第二个从索引 0 开始到今天。然后将这两个数组合并在一起,你就得到了你想要的。

您可以使用 uksort(), date() and strtotime() 重新排列数组。

function sort_week_days( $t1, $t2 ) {
        $weekdays = array( 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' );
        foreach ( $weekdays as $key => $value ) {
            $weekdays[ $key ] = date( 'w', strtotime( $value ) );
        }

        $t1_time = date( 'w', strtotime( strtolower( $t1 ) ) );
        $t2_time = date( 'w', strtotime( strtolower( $t2 ) ) );

        return array_search( $t1_time, $weekdays ) - array_search( $t2_time, $weekdays );
    }

您需要按如下方式传递您的随机数组:

usort($jour_planning, "sort_week_days");

然后你可以在下面的代码中使用你的数组:

$day = date('w'); // You need to change to date('w', '-1day'); get result from today.

for ($i=0; $i <= $day ; $i++) {
   array_push($jour_planning, array_shift($jour_planning));
}   

您可以使用此功能对工作日进行排序,并且可以选择包含第一天。

function sort_days($day_to_start = 'monday', $include_first_day = true)
{
    $final_days_planning = [];
    $rest_days = [];
    $first_day_found = 0;
    foreach ([ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ] as $day)
    {
        if (!$first_day_found){
            if ($day_to_start == $day){
                $first_day_found = 1;
                if($include_first_day){
                    $final_days_planning[] = $day;
                }
                else
                {
                    $rest_days[] = $day;
                }
            }
            else
            {
                $rest_days[] = $day;
            }
        } else{
            $final_days_planning[] = $day;
        }
    }
    return array_merge($final_days_planning, $rest_days);
}

试一试:

$days = sort_days('wednesday');
print_r($days);

您可以使用 in_array 查找数组中存在的天数并添加到新数组中。

$jour_planning=["saturday","wednesday","friday","thursday"];

public function getSortedDays($jour_planning){     
    $sorted_days=[];   
    for($i=1;$i<=7;$i++){
        $day = date("l", strtotime("+$i days"));
        $_day = strtolower($day);
        if(in_array($_day, $jour_planning)){
            $sorted_days[] = $_day;
        }
    }
    return $sorted_days;
}

$sorted_days = getSortedDays($jour_planning);

print_r($sorted_days); //Array ( [0] => wednesday [1] => thursday [2] => friday [3] => saturday )