根据php中的日期时间对数组进行排序

发布于 2024-12-15 03:43:50 字数 2056 浏览 0 评论 0原文

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] => 
                )

我想根据[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] => 
                )

有谁知道该怎么做吗?谢谢!

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] => 
                )

I want to sort the above array based on the [dateTime], the final output should be:

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] => 
                )

Is there anyone know how to do it? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

百思不得你姐 2024-12-22 03:43:50

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;
});

< a href="http://www.php.net/manual/en/class.datetime.php" rel="noreferrer">DateTime 类重载了比较运算符 (< , >, <代码>==)。

Use usort() function with custom comparator:

$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;
});

DateTime class has overloaded comparison operators (<, >, ==).

时光与爱终年不遇 2024-12-22 03:43:50

就性能而言,这种使用 array_multisort 的方法非常高效

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

For performance, this method using array_multisort is very efficient:

$ord = array();
foreach ($array as $key => $value){
    $ord[] = strtotime($value['dateTime']);
}
array_multisort($ord, SORT_ASC, $array);
print_r($array);
亢潮 2024-12-22 03:43:50

使用 spaceship 运算符对 DateTime 对象进行简单排序:

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

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

var_dump($list);

Simple sorting for DateTime objects with the spaceship operator:

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

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

var_dump($list);
冰雪之触 2024-12-22 03:43:50

使用 uasort() 和自定义排序回调应该可以做到这一点:

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

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

uasort($arr, 'cmp');

uasort() 保留数组的键,您可以使用 usort() 相反,如果您不需要这种情况发生。

Using uasort() with a custom sort callback should do it:

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

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

uasort($arr, 'cmp');

uasort() preserves your array's keys, you can use usort() instead if you don't need that to happen.

三五鸿雁 2024-12-22 03:43:50

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

对于日期时间降序:

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

对于日期时间升序:

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

For php array and object this example is helpful for sorting...

For datetime descending:

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

For datetime ascending:

usort($response_data['callback'], function($a, $b) {
   return strtotime($a->view_date) - strtotime($b->view_date);
});
云雾 2024-12-22 03:43:50

使用 array_multisort :

 <?php
        $myarray=array(
                    0 => array
                        (
                            'dateTime' => '2011-10-18 00:0:00',
                            'chanl1' => '20.7',
                            'chanl2' => '45.4',
                            'chanl3' => '',
                        ),

                    1 => array
                        (
                            'dateTime' => '2011-10-18 00: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' => ''
                        ));

            foreach($myarray as $c=>$key) {
                    $dateTime[] = $key['dateTime'];
                $chanl1[] = $key['chanl1'];
                    $chanl2[] = $key['chanl2'];
                    $chanl3[] = $key['chanl3'];
            }

        array_multisort($dateTime,SORT_ASC,SORT_STRING,$myarray);   
        print_r($myarray);

Use array_multisort :

 <?php
        $myarray=array(
                    0 => array
                        (
                            'dateTime' => '2011-10-18 00:0:00',
                            'chanl1' => '20.7',
                            'chanl2' => '45.4',
                            'chanl3' => '',
                        ),

                    1 => array
                        (
                            'dateTime' => '2011-10-18 00: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' => ''
                        ));

            foreach($myarray as $c=>$key) {
                    $dateTime[] = $key['dateTime'];
                $chanl1[] = $key['chanl1'];
                    $chanl2[] = $key['chanl2'];
                    $chanl3[] = $key['chanl3'];
            }

        array_multisort($dateTime,SORT_ASC,SORT_STRING,$myarray);   
        print_r($myarray);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文