繁体   English   中英

按日期时间在 PHP 中的数组中对 arrays 进行排序

[英]Sorting arrays within an array in PHP by datetime

我目前在 PHP 中遇到问题,我想按创建日期对这些帖子进行排序,以便它们可以按降序显示。 我一直在寻找一个 PHP function 来做到这一点,但没有运气。

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

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

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

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

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

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

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 联机帮助页

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM