Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Sorting PHP by timestamp

I have problem ordering an array in the way I want.

This is an example output of my array:

# 159 rows in total
Array
(
    [0] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025723
            [3] => /fcTGqQGD
        )
 
    [1] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025723
            [3] => /YQNk8yqa
        )
 
    [2] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025723
            [3] => /u2BNPrad
        )
 
    [3] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025722
            [3] => /d/blogdialain.com
        )
 
    [4] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025722
            [3] => /d/shopcraze.com
        )
 
    [5] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025722
            [3] => /domains_archive/175
        )
 
    [6] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025722
            [3] => /d/togou.com
        )
 
    [7] => Array
        (
            [0] => pastebin
            [1] =>  
            [2] => 1305025722
            [3] => /W6NafmJa
        )
)

Complete array data here: http://pastebin.com/GJNBmqL7

Data comes from various database at once, so I'm limited in the way that I put the data into the array.

The [2] always contains a linux timestamp. Now I want the entire array to be ordered by that timestamp, with the lowest value first.

How can I get that done?

like image 377
Mr.Boon Avatar asked May 10 '11 11:05

Mr.Boon


People also ask

How to sort time array in php?

php $array = [ '11-01-2012', '01-01-2014', '01-01-2015', '09-02-2013', '01-01-2013' ]; function sortFunction( $a, $b ) { return strtotime($a) - strtotime($b); } usort($array, "sortFunction"); var_dump( $array ); ?>

How to sort multidimensional array php?

Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.

How does php arrange dates in ascending order?

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); ?>


2 Answers

here an array_multisort example:

foreach ($array as $key => $node) {
   $timestamps[$key]    = $node[2];
}
array_multisort($timestamps, SORT_ASC, $array);
like image 170
mjspier Avatar answered Oct 12 '22 21:10

mjspier


You can do this simply by using a custom sort. So assuming your datestamp is always index 2:

function sortArray($a1, $a2){
    if ($a1[2] == $a2[2]) return 0;
    return ($a1[2] > $a2[2]) ? -1 : 1;
}

usort($array, "sortArray");
like image 29
Coin_op Avatar answered Oct 12 '22 22:10

Coin_op