Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

questions on sort array by time in php

---array $points----

Array
    (
        [0] => Array
            (
                [0] => 2011-10-02 05:30:00
                [1] => 20
            )

        [1] => Array
            (
                [0] => 2011-10-04 09:30:00
                [1] => 12
            )

        [2] => Array
            (
                [0] => 2011-10-01 13:30:00
                [1] => 25
            )

        [3] => Array
            (
                [0] => 2011-10-03 02:30:00
                [1] => 31
            )

    )

I have an array at above and would like to sort this array by time. Then I used the code as following to sort and result is correct. However, if I changed the code time[$key] = $val[0] to $time = $val[0], the result is wrong.

Is there anyone can explain this to me? Many thanks!

foreach($points as $key=>$val){

        $time[$key] = $val[0];

        array_multisort($time, SORT_ASC, $points);
    }
like image 485
Acubi Avatar asked Jan 03 '12 16:01

Acubi


People also ask

How do I sort an array of dates in PHP?

php function compareDates($date1, $date2){ return strtotime($date1) - strtotime($date2); } $dateArr = array("2019-11-11", "2019-10-10","2019-08-10", "2019-09-08"); usort($dateArr, "compareDates"); print_r($dateArr); ?>

How does Usort work in PHP?

The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array in a new manner. This function assigns new integral keys starting from zero to the elements present in the array and the old keys are lost.

Which function sort multiple array at once in PHP?

The array_multisort() is an inbuilt function in PHP which is used to sort multiple arrays at once or a multi-dimensional array with each individual dimension.

Can counting sort handle duplicates?

This algorithm may also be used to eliminate duplicate keys, by replacing the Count array with a bit vector that stores a one for a key that is present in the input and a zero for a key that is not present.


2 Answers

The function uasort() takes a comparison callback function. You can use this to compare two timestamps.

$arr = array(
        array('2011-10-02 05:30:00','20'),
        array('2011-10-04 09:30:00','12'),
        array('2011-10-01 13:30:00','25'),
        array('2011-10-03 02:30:00','31')
);

function timecomp($a,$b)
{
    // Subtracting the UNIX timestamps from each other.
    // Returns a negative number if $b is a date before $a,
    // otherwise positive.
    return strtotime($b[0])-strtotime($a[0]);
}
uasort($arr,'timecomp');

print_r($arr);

The above code will return

(
    [1] => Array
        (
            [0] => 2011-10-04 09:30:00
            [1] => 12
        )

    [3] => Array
        (
            [0] => 2011-10-03 02:30:00
            [1] => 31
        )

    [0] => Array
        (
            [0] => 2011-10-02 05:30:00
            [1] => 20
        )

    [2] => Array
        (
            [0] => 2011-10-01 13:30:00
            [1] => 25
        )

)
like image 67
kba Avatar answered Sep 17 '22 21:09

kba


array_multisort sorts more than one array at once. However, it works on an array of columns, so the foreach loop is needed to get a column of the times. After building up this list, you can then perform the multisort. The $points array is ordered according to the indices in $times, as per this example in the docs.

However, you don't need to perform the sort inside the foreach, as that means the sort happens 4 times (in your example). It only needs to happen once:

foreach ($points as $key => $val) {
    $time[$key] = $val[0];
}

array_multisort($time, SORT_ASC, $points);
like image 39
cmbuckley Avatar answered Sep 21 '22 21:09

cmbuckley