0

I have an array as follows:

$array = array (
    array(  'Day' => 'Monday',
            'Start' => '0830',
            'End' => '1730'),

    array(  'Day' => 'Monday',
            'Start' =>'1730',
            'End' => '2130'),

    array(  'Day' => 'Tuesday',
            'Start' => '0600',
            'End' => '1100'),

    array(  'Day' => 'Tuesday',
            'Start' => '0600',
            'End' => '0900'),           
);

I'm trying to work out how to sort this so the results are sorted first by Day and then by the earliest Start, then End.

Using the above array, the results would be:

Monday 0830-1730
Monday 1730-2130

Tuesday 0600-0900
Tuesday 0600-1100

Can any advise the best way to do this. Thanks

3 Answers 3

5

so, as @Daniel wrote just use usort function also you will need to check how you want to represent Day as int - check date function manual for that - in my solution N format is used, so Monday - 1 ... Sunday - 7 logic for sorting is just a https://en.wikipedia.org/wiki/Polynomial

<?php

$array = array (
    array(  'Day' => 'Monday',
            'Start' => '0830',
            'End' => '1730'),

    array(  'Day' => 'Monday',
            'Start' =>'1730',
            'End' => '2130'),

    array(  'Day' => 'Tuesday',
            'Start' => '0600',
            'End' => '1100'),

    array(  'Day' => 'Tuesday',
            'Start' => '0600',
            'End' => '0900'),           
);

usort($array, function($a, $b) {
    return (date("N", strtotime($a['Day'])) <=> date("N", strtotime($b['Day']))) * 100 +
           ($a['Start'] <=> $b['Start']) * 10 +
           ($a['End'] <=> $b['End']);
});

var_dump($array);

demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - That seems to do exactly what I need. Thanks for the links to date and usort.
you are welcome mate! that's how SO works ^_^ peace!
2

The best way would be to use uasort (https://www.php.net/manual/function.uasort.php), which allows you to implement a custom compare function.

Comments

1

You can use a usort with the spaceship operater, placing your comparible keys in a arrays in order of precedence:

A flipped array is there to get numeric values 0-4 for the weekdays.

<?php

$array = array (
    array(  'Day' => 'Monday',
            'Start' => '0830',
            'End' => '1730'),

    array(  'Day' => 'Monday',
            'Start' =>'1730',
            'End' => '2130'),

    array(  'Day' => 'Tuesday',
            'Start' => '0600',
            'End' => '1100'),

    array(  'Day' => 'Tuesday',
            'Start' => '0600',
            'End' => '0900'),           
);

usort($array, function($a, $b) {
    $dotw = array_flip(['Monday', 'Tuesday', 'Wednesday', 'Thursday','Friday']);
    return
        [$dotw[$a['Day']], $a['Start'], $a['End']]
        <=>
        [$dotw[$b['Day']], $b['Start'], $b['End']];
});
var_export($array);

Output:

array (
    0 => 
    array (
    'Day' => 'Monday',
    'Start' => '0830',
    'End' => '1730',
    ),
    1 => 
    array (
    'Day' => 'Monday',
    'Start' => '1730',
    'End' => '2130',
    ),
    2 => 
    array (
    'Day' => 'Tuesday',
    'Start' => '0600',
    'End' => '0900',
    ),
    3 => 
    array (
    'Day' => 'Tuesday',
    'Start' => '0600',
    'End' => '1100',
    ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.