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
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.
Outputs :
Answer
Solution:
You can rearrange your array using
,
and
.
You need to pass your random array as below :
Then you can used your array in below code :
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 the
datetime()
function, see @Cid's answer), so: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.
Answer
Solution:
You can use this function to sort week days and it's has option to include the first day.
To try it:
Answer
Solution:
You can use in_array to find the days present in your array and add to new array.
Answer
Solution: