Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate an array with weekday names as keys to start with tomorrow

I have the following array:

[
     [
         'schedules' => [
             'monday' => 1,
             'tuesday' => 1,
             'wednesday' => 1,
             'thursday' => 1,
             'friday' => 1,
             'saturday' => 0,
             'sunday' => 1,
         ]
    ]
]

I'd like to rotate the elements of this array with the first key being the tomorrow. Let's say today was Wednesday, I'd want my array to look like this:

[
     [
         'schedules' => [
             'thursday' => 1,
             'friday' => 1,
             'saturday' => 0,
             'sunday' => 1,
             'monday' => 1,
             'tuesday' => 1,
             'wednesday' => 1,
         ]
    ]
]

I already have the weekday available (e.g. a string 'thursday'). It gets passed into the function that I'm working with.

like image 576
kwikness Avatar asked Jul 16 '12 02:07

kwikness


6 Answers

This Answer allows which Day of the Week to start at, and dynamically handles how days are labeled.

This is simply an improved solution using uksort(), date(), and strtotime().

function sort_days( $term1, $term2 ) {
    $weekdays = array( 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' );
    foreach ( $weekdays as $key => $value ) {
        $weekdays[ $key ] = date( 'N', strtotime( $value ) );
    }

    $term1_time = date( 'N', strtotime( strtolower( $term1 ) ) );
    $term2_time = date( 'N', strtotime( strtolower( $term2 ) ) );

    return array_search( $term1_time, $weekdays ) - array_search( $term2_time, $weekdays );
}

Sorting keys with uksort()

$uk_days = array(
    'thursday'  => 1,
    'monday'    => 1,
    'saturday'  => 1,
    'wednesday' => 1,
    'sunday'    => 1,
    'friday'    => 1,
    'tuesday'   => 1,
);
uksort($uk_days, "sort_days");
var_dump($uk_days);

Sorting keys with usort()

$u_days = array(
    'thursday',
    'monday',
    'saturday',
    'wednesday',
    'sunday',
    'friday',
    'tuesday',
);
usort($u_days, "sort_days");
var_dump($u_days);
like image 131
EkoJR Avatar answered Nov 15 '22 14:11

EkoJR


you can do like this to achieve this

$week = [
    'sunday',
    'monday',
    'tuesday',
    'wednesday',
    'thursday',
    'friday',
    'saturday',
];

  $day = date('w'); // weekday as integer(3 for Wednesday)

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

  $result = $week;

Result (if today is Wednesday):

    Array
    (
        [0] => thursday
        [1] => friday
        [2] => saturday
        [3] => sunday
        [4] => monday
        [5] => tuesday
        [6] => wednesday
    )
like image 41
ajuchacko91 Avatar answered Nov 15 '22 14:11

ajuchacko91


If you convert the day to a number 0-6 you can array_shift and array_push that many times to move the previous days to the end of the array.

like image 43
Adam Bergmark Avatar answered Nov 15 '22 12:11

Adam Bergmark


Try using uksort(). You can compare dates in the callback function described in the link.

For example:

function compare($a, $b) { 
  date(strtotime($a)) - date(strtotime($b)); 
}

uksort($array, "compare"); 

Here's proof of it working

like image 21
Marty Cortez Avatar answered Nov 15 '22 14:11

Marty Cortez


You can get the day of the week via the date function. Sunday is 0, Monday is 1, and so forth.

$weekday = date("w");

Then, I suggest using the uksort function to sort the array relative to its keys, which takes a callback function as a sorting guideline.

uksort($schedules, function ($a, $b) use ($weekday) {
    // convert each day to a number 0-6 and compare to $weekday
});
like image 27
FThompson Avatar answered Nov 15 '22 14:11

FThompson


First you need a list in the correct order, which you can grab through the DateTime class. Then, as you loop over the old array, use the correct order as the key to sort the array, like so:

function sort_by_weekday( $input_array) {
    $now = new DateTime( "tomorrow", new DateTimeZone('America/New_York'));
    $interval = new DateInterval( 'P1D'); // 1 Day interval
    $period = new DatePeriod( $now, $interval, 7); // 7 Days

    $sorted_array = array();
    foreach( $period as $day) {
        $weekday = strtolower( $day->format( 'l'));

        foreach( $input_array as $key => $value) { 
            $sorted_array[ $key ][ $weekday ] = $value[ $weekday ]; 
        }
    }
    return $sorted_array;
}

You can see it working in the demo.

like image 20
nickb Avatar answered Nov 15 '22 14:11

nickb