Php 按日期对多维数组排序
我有一个数组[all_comments],其中包含3个数组-[title]、[content]和[date] 我想按[日期]对[所有评论]进行排序。我在这个论坛上尝试了很多日期比较功能,但似乎没有什么对我有用。感激地接受任何帮助或建议-谢谢 以下是我一直在尝试的函数类型:Php 按日期对多维数组排序,php,sorting,date,multidimensional-array,Php,Sorting,Date,Multidimensional Array,我有一个数组[all_comments],其中包含3个数组-[title]、[content]和[date] 我想按[日期]对[所有评论]进行排序。我在这个论坛上尝试了很多日期比较功能,但似乎没有什么对我有用。感激地接受任何帮助或建议-谢谢 以下是我一直在尝试的函数类型: function date_compare($a, $b) { $t1 = strtotime($a['date']); $t2 = strtotime($b['date']); return $t1
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 />";
}
答案更新: 数组的结构不正确。首先对其进行以下修改:
<?php
$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",
),
);
$newArr = array();
foreach($all_comments as $masterKey => $masterValue) {
foreach ($masterValue as $key => $value) {
$newArr[$key][$masterKey] = $value;
}
}
var_dump($newArr);
非常感谢您优雅的解决方案,并为我迟来的回复道歉。我通过首先合并标题、内容和日期数组找到了解决方案。然后,日期排序功能开始工作,我能够打印出合并数组中每个I元素的标题、日期和内容元素。@BiswadipDasgupta Welcome:)别忘了接受答案
<?php
$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",
),
);
$newArr = array();
foreach($all_comments as $masterKey => $masterValue) {
foreach ($masterValue as $key => $value) {
$newArr[$key][$masterKey] = $value;
}
}
var_dump($newArr);