1

I have array like below, how to sort data with a combination of date and time descending?

$manifest = array(
    array("date"=>2019-02-21,"time"=>04:49:54,"desc"=>"arrived in indonesia"),
    array("date"=>2019-02-25,"time"=>04:02:21,"desc"=>"arrived in soppeng"),
    array("date"=>2019-02-22,"time"=>01:42:51,"desc"=>"arrived in makassar"),
    array("date"=>2019-02-22,"time"=>02:42:51,"desc"=>"arrived in makassar gateway"),
);

Expected result:

$manifest = array(
    array("date"=>2019-02-25,"time"=>04:02:21,"desc"=>"arrived in soppeng"),
    array("date"=>2019-02-22,"time"=>02:42:51,"desc"=>"arrived in makassar gateway"),
    array("date"=>2019-02-22,"time"=>01:42:51,"desc"=>"arrived in makassar"),
    array("date"=>2019-02-21,"time"=>04:49:54,"desc"=>"arrived in indonesia")
    );

Thanks For Help

4
  • Show us, what you have tried so far in a Minimal, Complete, and Verifiable example and you'll get help Commented Feb 21, 2019 at 6:59
  • @AbuAyyub Did you try use array_multisort as in my post? Commented Feb 21, 2019 at 8:51
  • Duplicate of stackoverflow.com/questions/17364127/… Commented Feb 21, 2019 at 9:05
  • @04fs this is not duplicate Commented Feb 21, 2019 at 9:22

4 Answers 4

2
$arr = $manifest;

usort($arr, function($a, $b) {
  $ad = new DateTime($a['date'] ." ". $a['time']);
  $bd = new DateTime($b['date']." ". $a['time']);

  if ($ad == $bd) {
    return 0;
  }

  return $ad < $bd ? 1 : -1;
});
Sign up to request clarification or add additional context in comments.

Comments

2

I solved my self, i am using

usort($manifest,function($a,$b){
   return strtotime($b['date']." ".$b['time']) - strtotime($a['date']." ".$a['time']);
});

Thanks for all to helping me.

1 Comment

You can mark your's answer as accepted so it will shown this issue has been resolve (or other answer if you prefer any)
1

You can use rsort() which will sort the array in descending order.

<?php
$manifest = array(
  array("date"=>"2019-02-21","time"=>"04:49:54","desc"=>"arrived in indonesia"),
  array("date"=>"2019-02-25","time"=>"04:02:21","desc"=>"arrived in soppeng"),
  array("date"=>"2019-02-22","time"=>"01:42:51","desc"=>"arrived in makassar"),
  array("date"=>"2019-02-22","time"=>"02:42:51","desc"=>"arrived in makassar gateway")
);

rsort($manifest);

echo '<pre>';print_r($manifest);echo '</pre>';
?>

Output:

Array
(
    [0] => Array
        (
            [date] => 2019-02-25
            [time] => 04:02:21
            [desc] => arrived in soppeng
        )

    [1] => Array
        (
            [date] => 2019-02-22
            [time] => 02:42:51
            [desc] => arrived in makassar gateway
        )

    [2] => Array
        (
            [date] => 2019-02-22
            [time] => 01:42:51
            [desc] => arrived in makassar
        )

    [3] => Array
        (
            [date] => 2019-02-21
            [time] => 04:49:54
            [desc] => arrived in indonesia
        )

)

Comments

1

How about array-multisort?

array_multisort(array_column($manifest, 'date'), SORT_DESC, array_column($manifest, 'time'), SORT_DESC, $manifest);

Simple as that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.