12

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?

Blaztix
  • 1,223
  • 1
  • 19
  • 28
Mr.Boon
  • 2,024
  • 7
  • 35
  • 48

8 Answers8

11

use usort that accepts custom function to sort arrays:

usort

your function may look something like:

function cmp($a, $b)
{
    if ($a[2] == $b[2]) {
        return 0;
    }
    return ($a[2] < $b[2]) ? -1 : 1;
}
Headshota
  • 21,021
  • 11
  • 61
  • 82
11

here an array_multisort example:

foreach ($array as $key => $node) {
   $timestamps[$key]    = $node[2];
}
array_multisort($timestamps, SORT_ASC, $array);
Alexxus
  • 893
  • 1
  • 11
  • 25
mjspier
  • 6,386
  • 5
  • 33
  • 43
8

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");
Coin_op
  • 10,568
  • 4
  • 35
  • 46
5

usort is the easiest to work with, but you can also check out array_multisort.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
0

This can be achieved with following code:

usort($variable, function ($a, $b) {
    if ($a['timestamp'] == $b['timestamp']) {
       return 0;
    }
    return ($a['timestamp'] < $b['timestamp']) ? 1 : -1;  
});

This will result in sorted array of $variable, assuming time-stamp is at

$variable[0]['timestamp'];
$variable[0]['timestamp'];

and so on.

Blaztix
  • 1,223
  • 1
  • 19
  • 28
0

Use usort and create your own function, http://php.net/manual/en/function.usort.php

Joshua - Pendo
  • 4,331
  • 6
  • 37
  • 51
0
function cmp($a, $b)
{
    if ($a[2] == $b[2]) {
        return 0;
    }
    return ($a[2] < $b[2]) ? -1 : 1;
}

$data = array( /* your data */ );

usort($data, "cmp");
hsz
  • 148,279
  • 62
  • 259
  • 315
0

It might be easier to insert the data into a single temporary table, then SELECT it, with ORDER BY timestamp

Abel Mohler
  • 785
  • 5
  • 12