基于php中的dateTime排序数组

Acu*_*ubi 20 php arrays sorting

Array
        (
            [0] => Array
                (
                    [dateTime] => 2011-10-18 0:0:00
                    [chanl1] => 20.7
                    [chanl2] => 45.4
                    [chanl3] => 
                )

            [1] => Array
                (
                    [dateTime] => 2011-10-18 0:15:00
                    [chanl1] => 20.7
                    [chanl2] => 45.4
                    [chanl3] => 
                )


            [2] => Array
                (
                    [dateTime] => 2011-10-18 00:14:00
                    [chanl1] => 20.7
                    [chanl2] => 33.8
                    [chanl3] => 
                )

            [3] => Array
                (
                    [dateTime] => 2011-10-18 00:29:00
                    [chanl1] => 20.6
                    [chanl2] => 33.9
                    [chanl3] => 
                )
Run Code Online (Sandbox Code Playgroud)

我想基于[dateTime]对上面的数组进行排序,最终的输出应该是:

Array
        (
            [0] => Array
                (
                    [dateTime] => 2011-10-18 0:0:00
                    [chanl1] => 20.7
                    [chanl2] => 45.4
                    [chanl3] => 
                )

            [1] => Array
                (
                    [dateTime] => 2011-10-18 00:14:00
                    [chanl1] => 20.7
                    [chanl2] => 33.8
                    [chanl3] => 
                )

            [2] => Array
                (
                    [dateTime] => 2011-10-18 0:15:00
                    [chanl1] => 20.7
                    [chanl2] => 45.4
                    [chanl3] => 
                )            

            [3] => Array
                (
                    [dateTime] => 2011-10-18 00:29:00
                    [chanl1] => 20.6
                    [chanl2] => 33.9
                    [chanl3] => 
                )
Run Code Online (Sandbox Code Playgroud)

有谁知道怎么做?谢谢!

Cro*_*zin 51

使用自定义比较器的usort()功能:

$arr = array(...);

usort($arr, function($a, $b) {
  $ad = new DateTime($a['dateTime']);
  $bd = new DateTime($b['dateTime']);

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

  return $ad < $bd ? -1 : 1;
});
Run Code Online (Sandbox Code Playgroud)

日期时间类已重载比较运算符(<,>,==).

  • @Crozin 这是一个常见的误解,认为 usort 必须返回 `-1,0,1`。就像手册中所述:*如果第一个参数被认为分别小于、等于或大于第二个参数,则比较函数必须返回一个小于、等于或大于零的整数。*这意味着任何值`&lt;=&gt;0`。 (3认同)
  • @Gordon:是的,我知道这个事实,但是如果我们操作的不是数字(在本例中为 DateTime),则返回 `±1` 是一种常见做法。 (3认同)
  • 我很高兴我们可以在 PHP 7 中使用宇宙飞船运算符 `&lt;=&gt;`。:) (3认同)
  • 这三元不倒退吗?除非我缺少一些愚蠢的东西,如果第一个值小于第二个值,那么你应该返回一个值<0.我认为它应该是```return $ ad <$ bd?-1:1;``` (2认同)
  • 注意:切换 -1 和 1 可将一般顺序更改为降序 (2认同)

Cli*_*ive 11

使用uasort()自定义排序回调应该这样做:

function cmp($a, $b) {
  if ($a['dateTime'] == $b['dateTime']) {
    return 0;
  }

  return ($a['dateTime'] < $b['dateTime']) ? -1 : 1;
}

uasort($arr, 'cmp');
Run Code Online (Sandbox Code Playgroud)

uasort()保留数组的键,usort()如果不需要,可以使用它.

  • @ h.coates是的,你是正确的我假设它是采样数据中给出的格式.为什么不呢? (2认同)

Use*_*407 10

为了提高性能,使用array_multisort的方法非常有效:

$ord = array();
foreach ($array as $key => $value){
    $ord[] = strtotime($value['dateTime']);
}
array_multisort($ord, SORT_ASC, $array);
print_r($array);
Run Code Online (Sandbox Code Playgroud)


小智 7

使用宇宙飞船运算符对 DateTime 对象进行简单排序:

$list = [
    new DateTime('yesterday'),
    new DateTime('today'),
    new DateTime('1 week ago'),
];

uasort($list, function ($a, $b) {
    return $a <=> $b;
});

var_dump($list);
Run Code Online (Sandbox Code Playgroud)


Man*_*ani 5

对于 php 数组和对象,此示例有助于排序...

对于日期时间降序:

usort($response_data['callback'], function($a, $b) {
   return strcasecmp(strtotime($b->view_date), strtotime($a->view_date));
});
Run Code Online (Sandbox Code Playgroud)

对于日期时间升序:

usort($response_data['callback'], function($a, $b) {
   return strtotime($a->view_date) - strtotime($b->view_date);
});
Run Code Online (Sandbox Code Playgroud)