0

I were using the "rsort" function to sort through the timestamps when the array only contained a timestamp and were not multidimensional.

So now the question is how do i approach this?

The array looks something similair to this

Array
(
    [0] => Array
        (
            [type] => post
            [id] => 1
            [timestamp] => 2017-08-12 21:03:22
            [poster] => 1
            [profile] => 1
            [post] => Testtttttinngggs
        )

    [1] => Array
        (
            [type] => post
            [id] => 2
            [timestamp] => 2017-08-12 21:03:18
            [poster] => 1
            [profile] => 5
            [post] => Hello you
        )

    [2] => Array
        (
            [type] => post
            [id] => 3
            [timestamp] => 2017-08-12 21:03:33
            [poster] => 1
            [profile] => 1
            [post] => Somesay timestamp is screwed
        )

    [3] => Array
        (
            [type] => post
            [id] => 4
            [timestamp] => 2017-08-12 21:28:54
            [poster] => 1
            [profile] => 1
            [post] => This is truely a teeest
        )

    [4] => Array
        (
            [type] => post
            [id] => 5
            [timestamp] => 2017-08-13 15:04:34
            [poster] => 1
            [profile] => 1
            [post] => Test test test test
        )

)
Javaish
  • 159
  • 10

3 Answers3

8

You can use array_multisort

array_multisort(array_column($list, 'timestamp'), SORT_ASC, $list);
Axalix
  • 2,614
  • 1
  • 18
  • 34
  • That's pretty slick. – Rob Ruchte Aug 13 '17 at 17:24
  • That was pretty neat – Javaish Aug 13 '17 at 17:27
  • 1
    @Javaish Rob Ruchte's answer will easily outperform this one because it doesn't require PHP to first extract the `timestamp` into a separate column array, and then execute the multisort ( `array_multisort` is itself a more complex function than Rob's simple comparator). This answer looks sexy, but is a lot less efficient. – BeetleJuice Aug 13 '17 at 17:38
  • @BeetleJuice run both approaches on my machine 1Mil times. My approach took 1.845 seconds. Rob's approach took 16.883182048798 seconds. You may be right though if you are about space complexity. `usort` is known as very and very slow. – Axalix Aug 13 '17 at 17:46
  • That's a shocker. Hang on let me test – BeetleJuice Aug 13 '17 at 17:47
  • @BeetleJuice https://pastebin.com/DZ9pgQbi – Axalix Aug 13 '17 at 17:55
  • Axalix you're right! Just ran 100,000 iterations of each approach and `array_multisort` is faster; **much** faster -- takes only about 1/7th the time. How could that be?? – BeetleJuice Aug 13 '17 at 17:56
  • PHP magic - I faced this issue a while ago and was also surprised. `array_multisort` is also nice because you can sort an array by many (_multiple_) fields like in `ORDER BY A ASC, B DESC, ...` sql syntax. – Axalix Aug 13 '17 at 17:58
  • 1
    @BeetleJuice The overhead of repeatedly invoking the comparator most likely… – deceze Aug 13 '17 at 18:01
1

You can use usort

usort($array, function($a, $b)
{
    if($a['timestamp']>$b['timestamp'])
    {
        return -1;
    }
    elseif($a['timestamp']<$b['timestamp'])
    {
        return 1;
    }

    return 0;
});
Rob Ruchte
  • 1,936
  • 1
  • 12
  • 14
1

Update

I was wrong. Axalix' answer runs a lot faster than mine and Rob Ruchte's. My tests:

$data = [
    ['timestamp'=> '2015-08-12', 'id'=>1],
    ['timestamp'=> '2017-07-13', 'id'=>2],
    ['timestamp'=> '2017-01-12', 'id'=>3],
];

function useUsort($data){
  usort($data,function($a,$b) {
    return strtotime($b['timestamp']) - strtotime($a['timestamp']);
  });
};
function useMultisort($data){
  array_multisort(array_column($data, 'timestamp'), SORT_DESC, $data);
};

$start = microtime(true);
for($i=1;$i<=100000;$i++) useUsort($data);
$t1 = microtime(true);
for($i=1;$i<=100000;$i++) useMultisort($data);
$t2 = microtime(true);

echo "usort:           ". round(($t1 - $start) * 1000) . " ms\n";
echo "array_multisort: ". round(($t2 - $t1) * 1000) . " ms\n";

My result:

usort:           2262 ms
array_multisort: 246 ms

Original answer

@Axalix' answer is nice but I would take a different approach. Because you only care about sorting by one field (timestamp), array_multisort is overkill as it was design to sort by multiple fields. I would do:

usort($data,function($a,$b) {
    return strtotime($b['timestamp']) - strtotime($a['timestamp']);
});

Live demo

This will easily outperform array_multisort because it doesn't require PHP to first extract the timestamp into a separate column array, and then execute the multisort (a more complex function than my simple comparator function) on it.

BeetleJuice
  • 33,709
  • 16
  • 78
  • 137