php - Sort array of days from a specific start date

331

I have this array of days in a random order :

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

If we are today a "tuesday", I would like to have this new array :

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

How can do that, with usort() ?

Regards, Vianney

172

Answer

Solution:

I would first sort the array (and avoid french and english naming :) ).

Then, loop that sorted array and store into 2 separates array the days before the expected one (including this one), and the days after.

And finally, merge both arrays.

There's certainly better ways to do that.

$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);

Outputs :

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"
}
339

Answer

Solution:

Because of your array is randomly ordered, there is no other way to order it correctly, then doing it by hand (you cannot order the array numerically or alphabetically - it can be done by calculating the days by thedatetime() function, see @Cid's answer), so:

/*
$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);

Firstly you have to search the current day, then add 1 to the index of this day and you have the day, that will be tomorrow. Now, when you have this index, you simply create 2 arrays: the first one starting from the day of tomorrow and the second one starting from index 0 to the day of today. Then merge this two arrays together and you have exactly what you wanted to have.

671

Answer

Solution:

You can use this function to sort week days and it's has option to include the first day.

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);
}

To try it:

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

Answer

Solution:

You can use in_array to find the days present in your array and add to new 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 )
489

Answer

Solution:

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

People are also looking for solutions to the problem: phpmailer - Php mailer body is empty

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.