【发布时间】:2017-02-18 13:15:18
【问题描述】:
我需要通过 getNotifications 函数中的值 'start_date' DESC 顺序对通知数组进行排序:
$posts_id_arr = getPoststIds($conn);
foreach ($posts_id_arr as $key => $val) {
$total_arr[$key] = [
'notification' => getNotifications($val['post_id'], $user_id, $conn)
];
}
$response_array = array('data' => $total_arr, 'more things' => $more_array);
echo json_encode($response_array);
由于 foreach 循环,现在订单是按帖子 ID 排序的。
data {
notification:
[
{
post_id: “1",
start_date: "2016-10-10 08:00:00",
},
{
post_id: “1",
start_date: "2016-10-10 12:00:00",
}
],
notification:
[
post_id: “2",
start_date: "2016-10-10 09:00:00",
},
{
post_id: “2",
start_date: "2016-10-10 13:00:00",
}
]
}
我需要它是 1:08:00、2:09:00、1:12:00、2:13:00
【问题讨论】:
-
我认为你应该在循环之后对数据进行排序,这会很有帮助并且需要更少的迭代。
-
@Gilad Adar。您是否尝试过我的解决方案。如果不尝试分享想法,如果您遇到任何障碍,请告诉我,以便我们纠正它。