【发布时间】:2026-02-05 20:40:01
【问题描述】:
我有一个数组 [all_cmets],其中包含 3 个数组 - [title]、[content] 和 [date]。
我想按 [日期] 对 [all_cmets] 进行排序。我在这个论坛上尝试了许多日期比较功能,但似乎没有什么对我有用。感谢您提供任何帮助或建议 - 谢谢!
这些是我一直在尝试的功能:
function date_compare($a, $b)
{
$t1 = strtotime($a['date']);
$t2 = strtotime($b['date']);
return $t1 - $t2;
}
这就是我的数组的样子:
[all_comments] = Array
(
[title] => Array
(
[0] => bis posted an update in the group Strategic Resourcing
[1] => bis posted a new activity comment
[2] => bis posted a new activity comment
[3] => bis posted an update
[4] => bis posted an update in the group Managing for Performance
)
[content] => Array
(
[0] => @david hey david the meeting earlier was very interesting - thanks!
[1] => Strategic Resourcing & Onboarding description.
[2] => And another one to the original reply
[3] => This is a sample reply.
[4] => This is a new entry - does it go to the forum?
)
[date] => Array
(
[0] => 13-11-2013 5:36:48
[1] => 24-10-2013 9:52:48
[2] => 12-11-2013 12:40:46
[3] => 14-11-2013 2:26:04
[4] => 13-11-2013 5:39:49
)
)
这就是我调用函数的方式:
usort($all_comments,"date_compare");
这就是我打印数组的方式:
for($k=0;$k<count($all_comments ['title']);$k++){
echo ($all_comments ['title'][$k]) . "<br />";
echo ($all_comments ['content'][$k]) . "<br />";
echo ($all_comments ['date'][$k]) . "<br /><br />";
echo "<br />";
}
排序后不打印,但未排序的数组打印正常。
我最终想要的是一个如下所示的排序数组:
[sorted_array] = Array
(
[0] => Array
(
bis posted an update
This is a sample reply
14-11-2013 2:26:04
)
[1] => Array
(
bis posted an update in the group Managing for Performance
This is a new entry - does it go to the forum?
13-11-2013 5:39:49
)
[2] = Array
(
bis posted an update in the group Strategic Resourcing
@david hey david the meeting earlier was very interesting - thanks!
13-11-2013 5:36:48
)
[3] => Array
(
bis posted a new activity comment
And another one to the original reply
12-11-2013 12:40:46
)
[4] => Array
(
bis posted a new activity comment
Strategic Resourcing & Onboarding description.
24-10-2013 9:52:48
)
)
【问题讨论】:
标签: php sorting date multidimensional-array