简体   繁体   English

如何根据日期以降序对数组进行排序

[英]How to sort array based on date in descending order

I need to sort array object based on date in descending order using php. 我需要使用php根据日期以降序对数组对象进行排序。 I tried following code, but it didn't get expected result. 我尝试了以下代码,但是没有得到预期的结果。 Can you suggest be to do this task. 您能建议做这个任务吗? Thanks in advance. 提前致谢。

$document_list = array( '01-14-2013', '01-12-2013', '04-05-2016' );
usort($document_list, array($this, "sortByDate"));

function sortByDate($a, $b) {
    return  strcmp($b['createdDate'], $a['createdDate']);
}

When i tried this code, it get wrong output. 当我尝试此代码时,输​​出错误。

For this approach you need to convert the date format & dates to timestamp. 对于这种方法,您需要将日期格式和日期转换为时间戳。

$document_list = array( '14-01-2013', '12-01-2013', '05-04-2016' );
$document_list= array_map(function($v) {
    return date('Y-m-d', strtotime($v));
}, $document_list);

function sortByDate($a, $b) {
    return  strtotime($b) - strtotime($a);
}

usort($document_list, "sortByDate");

$document_list= array_map(function($v) {
    return date('m-d-Y', strtotime($v));
}, $document_list);

Output 输出量

array(3) {
  [0]=>
  string(10) "04-05-2016"
  [1]=>
  string(10) "01-14-2013"
  [2]=>
  string(10) "01-12-2013"
}
$document_list = array( '01-14-2013', '01-12-2013', '04-05-2016' );
usort($document_list, array($this, "sortByDate"));

function sortByDate($a, $b) {
    if(strtotime($b['createdDate'])<strtotime($a['createdDate'])){
        return $a['createdDate'];
    }else{
        return $b['createdDate'];
    }
}

Try this 尝试这个

Yo can do some elegant like this using: (usort: Sort an array by values using a user-defined comparison function 可以使用以下方法做一些优雅的事情:(usort:使用用户定义的比较函数按值对数组进行排序

<?php
     // Sort the multidimensional array
     usort($document_list, "custom_sort");
     // Define the custom sort function
     function custom_sort($a,$b) {
          return $a['some_sub_var']>$b['some_sub_var'];
     }
?>

Personally, I found sorting array by date a non-scalable option due to internal collisions of datetime format. 就个人而言,由于datetime格式的内部冲突,我发现按日期排序数组是不可缩放的选项。 Below may not answer the question to full extent, but may help if one works with array fetched from a DB . 以下内容可能无法完全回答该问题,但如果有人使用从DB中提取的数组 ,则可能会有所帮助

Suppose that in your table there is a primary auto-increment "id" column, then you can always ORDER BY id_column DESC. 假设表中有一个主要的自动递增“ id”列,那么您始终可以按ORDER BY id_column DESC。 You will receive same result as sorting by date - but much faster. 您将获得与按日期排序相同的结果-但速度更快。

If for some reason you can't use ORDER BY id DESC directly via SQL, just restructure array in php via these functions depending on your case. 如果由于某种原因您不能直接通过SQL使用ORDER BY id DESC,只需根据您的情况通过这些函数在php中重组数组。


As a side note: to preview your experiments with above php sorting functions, I strongly recommend visualizing the array display via such snippet that I found somewhere on outskirts of php.net - 附带说明:为了预览使用上述php排序功能的实验,我强烈建议您通过php.net郊区的某个片段来可视化数组显示-

function visual_arr($arr){
    echo '<pre>';
    print_r($arr);
    echo  '</pre>';
}

to call it - just use this somewhere in your projects' files: visual_arr ($the_array_you_want_to_display_in_human_radable_format). 调用它-只需在项目文件的某处使用它即可:visual_arr($ the_array_you_want_to_display_in_human_radable_format)。 Obviously choose a shorter name for your array ^^ 显然为您的数组选择一个较短的名称^^

Hope this helps. 希望这可以帮助。

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

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