php - 按日期时间对 PHP 中数组中的数组进行排序

标签 php arrays datetime sorting

我目前在 PHP 中遇到一个问题,我想按创建日期对这些帖子进行排序,以便它们可以按降序显示。我一直在寻找一个 PHP 函数来执行此操作,但一直没有成功。

有没有简单的解决方案?任何想法将不胜感激:)

array
      0 => 
        array
          'post_id' => string '1' (length=1)
          'user_id' => string '3' (length=1)
          'post' => string 'this is a post' (length=14)
          'created' => string '2012-04-05 20:11:38' (length=19)
     1 => 
        array
          'post_id' => string '2' (length=1)
          'user_id' => string '2' (length=1)
          'post' => string 'this is a post' (length=14)
          'created' => string '2012-04-05 20:11:38' (length=19)
     2 => 
        array
          'post_id' => string '3' (length=1)
          'user_id' => string '5' (length=1)
          'post' => string 'this is a post' (length=14)
          'created' => string '2012-04-05 20:11:38' (length=19)

最佳答案

试试这个:

<?php
$a=array(
      0 => 
        array(
          'post_id' => '1',
          'user_id' => '3',
          'post' => 'this is a post',
          'created' => '2012-04-05 20:11:40'
          ),
     1 => 
        array(
          'post_id' => '2',
          'user_id' => '2',
          'post' => 'this is a post',
          'created' => '2012-04-05 20:11:39'
          ),
     2 => 
        array(
          'post_id' => '3',
          'user_id' => '5',
          'post' => 'this is a post',
          'created' => '2012-04-05 20:11:38'
          )
);
function cmp($a,$b){
    return strtotime($a['created'])<strtotime($b['created'])?1:-1;
};

uasort($a,'cmp');
print_r($a);
?>

关于php - 按日期时间对 PHP 中数组中的数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10034753/

相关文章:

php - 每次我尝试使用 Swift 和 Alamofire 将数据发布到 MySQL 数据库时,数据都显示为空?

javascript - 了解获取数组中第二低和第二高的值

javascript - 一次显示多个数据点: Highcharts

datetime - 如何获取当前时间作为日期时间

php - 在 PHP 中将一种日期格式转换为另一种日期格式

c# - 汇总日期时间

javascript - 使用javascript有条件地格式化php输出

PHP 5.6 不会运行 CURL

动态页面上的 PHP 分页无法正常工作

actionscript-3 - 无法跳出嵌套的 for 循环