首先按日期键对数组值进行排序,然后按时间键对数组值进行排序

时间:2019-09-19 13:50:07

标签: php arrays sorting date

我有这个多维数组:

[
    {
        "Id": 14,
        "ClassDate": "2019-09-20T00:00:00",
        "StartTime": "06:00:00"
    },
    {
        "Id": 27,
        "ClassDate": "2019-09-20T00:00:00",
        "StartTime": "07:45:00"
    },
    {
        "Id": 144,
        "ClassDate": "2019-09-21T00:00:00",
        "StartTime": "05:00:00"
    },
    {
        "Id": 170,
        "ClassDate": "2019-09-21T00:00:00",
        "StartTime": "08:45:00"
    },

]

我想按日期排序此数组值,然后按时间排序相同的日期值。

这是我所拥有的代码,它仅按日期对值进行排序

function cmp($a, $b) {
    $a = strtotime($a['ClassDate']);
    $b = strtotime($b['ClassDate']);

    if ($a == $b) {  return 0;  }
    return ($a < $b) ? -1 : 1;  
}   usort($result_array, "cmp");

有人对此有解决方案吗?我敢肯定我是做到这一点的百万分之一...

4 个答案:

答案 0 :(得分:2)

您只需合并日期和时间,然后比较字符串即可节省很多工作。

function cmp($a, $b){
    return strcmp($a["ClassDate"] . $a["StartTime"], $b["ClassDate"] . $b["StartTime"]);    
}

usort($result_array,'cmp');

答案 1 :(得分:1)

您快到了。将时间StartTime添加到您的ClassDate中,然后进行比较以进行排序。

function cmp($a, $b) {
    $a = date('Y-m-d H:i:s', strtotime($a['StartTime'], strtotime($a['ClassDate'])));
    $b = date('Y-m-d H:i:s', strtotime($b['StartTime'], strtotime($b['ClassDate'])));

    if ($a == $b) {  return 0;  }
    return ($a < $b) ? -1 : 1;  
}   

usort($result_array, "cmp");

答案 2 :(得分:1)

按日期排序,与飞船操作员的时间递增(我重新排序了您的初始数据,因为它看起来已经被排序了):

<?php

$json=<<<JSON
[
    {
        "Id": 170,
        "ClassDate": "2019-09-21T00:00:00",
        "StartTime": "08:45:00"
    },
    {
        "Id": 14,
        "ClassDate": "2019-09-20T00:00:00",
        "StartTime": "06:00:00"
    },
    {
        "Id": 144,
        "ClassDate": "2019-09-21T00:00:00",
        "StartTime": "05:00:00"
    },
    {
        "Id": 27,
        "ClassDate": "2019-09-20T00:00:00",
        "StartTime": "07:45:00"
    }
]
JSON;

$data = json_decode($json, true);

function cmp($a, $b) {
    $a_date = $a['ClassDate'];
    $b_date = $b['ClassDate'];
    $a_time = $a['StartTime'];
    $b_time = $b['StartTime'];

    $cmp = $a_date <=> $b_date;
    if($cmp === 0)
        $cmp = $a_time <=> $b_time;

    return $cmp; 
}

usort($data, "cmp");
var_export($data);

输出:

array (
  0 => 
  array (
    'Id' => 14,
    'ClassDate' => '2019-09-20T00:00:00',
    'StartTime' => '06:00:00',
  ),
  1 => 
  array (
    'Id' => 27,
    'ClassDate' => '2019-09-20T00:00:00',
    'StartTime' => '07:45:00',
  ),
  2 => 
  array (
    'Id' => 144,
    'ClassDate' => '2019-09-21T00:00:00',
    'StartTime' => '05:00:00',
  ),
  3 => 
  array (
    'Id' => 170,
    'ClassDate' => '2019-09-21T00:00:00',
    'StartTime' => '08:45:00',
  ),
)

答案 3 :(得分:0)

我在这里找到了一些东西:Sort array using multiple criteria in PHP。并根据我的需要编辑了解决方案,这是有效的解决方案。玩<>符号可获得升/降序值。

function order_by_score_endgame($a, $b){
   if ($a['ClassDate'] == $b['ClassDate'])
    {
      // score is the same, sort by endgame
      if ($a['StartTime'] > $b['StartTime']) return 1;
    }
    // sort the higher score first:
    return $a['ClassDate'] > $b['ClassDate'] ? 1 : -1;
   }
   usort($result_array, "order_by_score_endgame");