Akhil Anand
Akhil Anand

Reputation: 17

How can i sort the below array by index(Date)?

Please sort the below array by its index(date). I want to sort the below array by ASC order by its date. After sorting the first one will be 2020-06-29 and its array, then 2020-06-30, I have tried more sorting mechanism but it could not solve the issue. Please help me to sort out this issue

Array(
                 [2020-07-01] => Array
                    (
                        [0] => Array
                            (
                                [slot] => 09:00 AM-11:00 AM
                                [is_available] => 1
                                [slot_id] => 29
                            )
    
                    )
                [2020-06-29] => Array
                    (
                        [0] => Array
                            (
                                [slot] => 02:16 AM-02:16 AM
                                [is_available] => 1
                                [slot_id] => 14
                            )
    
                    )
    
                [2020-06-30] => Array
                    (
                        [0] => Array
                            (
                                [slot] => 09:00 AM-06:00 PM
                                [is_available] => 1
                                [slot_id] => 15
                            )
    
                    )
    
                [2020-07-02] => Array
                    (
                        [0] => Array
                            (
                                [slot] => 10:00 AM-05:00 PM
                                [is_available] => 1
                                [slot_id] => 35
                            )
    
                    )
    
                [2020-07-03] => Array
                    (
                        [0] => Array
                            (
                                [slot] => 10:00 AM-03:00 PM
                                [is_available] => 1
                                [slot_id] => 36
                            )
    
                    )
    
            )

Upvotes: 0

Views: 201

Answers (2)

Lorenzo Canavaggio
Lorenzo Canavaggio

Reputation: 331

Try ksort

here is a short example :

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>

https://www.php.net/manual/en/function.ksort.php

EDIT :

Since ksort seems to not be working, you should use uksort.

function date_compare($a, $b)
{
    $t1 = strtotime($a);
    $t2 = strtotime($b);
    return $t1 - $t2;
}    
uksort($data, 'date_compare');

here is a working example :

https://repl.it/repls/FoolhardyDrabPolygon

Upvotes: 1

saonnet
saonnet

Reputation: 110

Step 1 : Create a function which accepts two dates ( strings ) as parameters, this function returns 1 if first date is greater than the second, it returns -1 if second date > first date or 0 if date's are equal. Working of functions starts from Step 2.

Step 2 : Toeknize the strings to get respective year, month and day of the two dates respectively. [ see : https://www.php.net/manual/en/book.tokenizer.php ]

Step 3 : If first date's year > second date's year, return 1 . If second date's year > first date's year return -1, if they are equal proceed to Step 4.

Step 4 : If first date's month > second date's month, return 1 . If second date's month > first date's month return -1, if they are equal proceed to Step 5.

Step 5 : If first date's day > second date's day, return 1 . If second date's day > first date's day return -1, if they are equal return 0.

Now use php's usort function and pass your function in it

[see : https://www.php.net/usort ]

Upvotes: 0

Related Questions