【发布时间】:2020-05-03 21:21:36
【问题描述】:
这是我想按日期降序排序的 php 数组内容。每行都以日期开头。每行会有多个单词:
$newarray = [
"8-06-2001 fish",
"10-09-2020 chips",
"3-07-2020 peas",
"9-09-2005 chicken",
"5-05-1999 veg",
"20-04-1998 sausage",
"1-04-1998 haddock",
"7-04-1998 cod",
"3-04-1998 curry",
"7-09-2005 burger",
"1-09-2005 cheese"
];
我试过这个功能,但对文本和日期都不好:
// DATE SORT FUNCTION
$compare_function = function($a,$b) {$a_timestamp = strtotime($a); // convert string date to a int timestamp
$b_timestamp = strtotime($b);
if ($a_timestamp > $b_timestamp) {return -1;}
elseif ($a_timestamp < $b_timestamp) {return 1;} else {return 0;}};
// USE FUNCTION
usort($newarray, $compare_function)
【问题讨论】:
-
给定示例数据,您希望它们在排序后的顺序是什么?
-
我想要最新的日期在顶部