【问题标题】:Sorting arrays within an array in PHP by datetime在 PHP 中按日期时间对数组中的数组进行排序
【发布时间】:2012-04-05 19:25:02
【问题描述】:

我目前在 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)

【问题讨论】:

  • 这是来自数据库吗?如果没有,为什么不呢?
  • 这个数组是从哪里加载的?
  • 是的,看起来很像 MySQL 查询行。 ORDER BY created DESC
  • 它来自数据库,但因为我必须分别抓取每个用户的帖子,我试图将它组合起来......有没有更好的方法来做类似...... WHERE user_id = MySQL 中的数组(2,3,5)?
  • 如果我们看不到您的查询,这很难说。 WHERE user_id IN (2,3,5) 在 MySQL 中有效

标签: php arrays datetime sorting


【解决方案1】:

试试这个:

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

【讨论】:

  • 如果$a 被认为小于$bcmp() 应该返回一个负值,如果$a 大于$b 则返回一个正值,如果它们相等则返回0。跨度>
  • 不需要strtotime。格式YYYY-MM-DD HH:MM:SS(24小时)只需要strcmp函数进行排序。
【解决方案2】:

按指定的mysql日期时间字段和顺序对记录/关联数组进行排序:

    function build_sorter($key, $dir='ASC') {
        return function ($a, $b) use ($key, $dir) {
            $t1=strtotime(is_array($a)?$a[$key]:$a->$key);
            $t2=strtotime(is_array($b)?$b[$key]:$b->$key);
            if($t1==$t2) return 0;
            return (str_to_upper($dir)=='ASC'?($t1 < $t2):($t1 > $t2)) ? -1 : 1;
        };
    }


    // $sort - key or property name 
    // $dir - ASC/DESC sort order or empty
    usort($arr, build_sorter($sort, $dir));

【讨论】:

    【解决方案3】:

    您可以使用usort() 函数,该函数允许您根据自己的标准对数组进行排序。

    function cmp($a, $b)
    {
        if ($a['created'] == $b['created']) {
            return 0;
        }
    
        return ($a['created'] < $b['created']) ? 1 : -1;
    }
    
    usort($myArray, "cmp");
    
    print_r($myArray);
    

    或者如果你想转换为时间:

    function cmp($a, $b)
    {
        if ($a['created'] == $b['created']) {
            return 0;
        }
    
        $aInt = strtotime($a['created']);
        $bInt = strtotime($b['created']);
    
        return ($aInt < $bInt) ? 1 : -1;
    }
    

    【讨论】:

      【解决方案4】:

      您可以使用自定义排序函数对数组进行排序,如下所示:

      function cmp($a, $b) {
          if($a['created'] < $b['created']) {
              return 1;
          } else if ($a['created'] > $b['created']) {
              return -1;
          } else  {
              // The only option left is that they are equal
              return 0;
          }
      }
      
      usort($array, cmp);
      

      有关 usort 的更多信息,请查看php manpage

      【讨论】:

      • 这将产生升序而不是降序。
      【解决方案5】:

      您可以使用strtotime() 将时间戳转换为整数。

      【讨论】:

      • 那将如何帮助对二维数组进行排序?
      • 嘿好吧,含糊的问题。我以为 OP 是在暗示他的邮票是 Y-d-m。
      • 如果日期是Y-d-m 格式,您不能轻易地将它们转换为时间戳。如果它们是Y-m-d 格式,则不需要转换它们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 2013-11-30
      相关资源
      最近更新 更多