gpt4 book ai didi

php - 如何根据php中的日期列对mysql结果数组进行降序排序?

转载 作者:行者123 更新时间:2023-11-29 02:47:45 24 4
gpt4 key购买 nike

我的母语不是英语,而且是 stackoverflow 的新手,所以如果我做错了什么,请纠正我。我的问题是我从 mysql 数据库名称的两个不同表中检索数据。

  1. Event
  2. Message

用于获取数据的代码如下。

$msgres=$ticket->getMessages();
$arr1 = array();
while($msg_row_one = db_fetch_array($msgres)){
$arr1[] = $msg_row_one;
}
$noteres=$ticket->getNotes();
$arr2 = array();
while ($msg_row_two = db_fetch_array($noteres)){
$arr2[] = $msg_row_two;
}

在此过程之后,我将合并两个数组。

$arr1[]

$arr2[]

像这样。

$merge = array_merge($arr1,$arr2);

然后我得到一个如下所示的数组。

Array
(
[0] => Array
(
[msg_id] => 7563
[ticket_id] => 1768
[messageId] =>
[msg_type] => R
[message] => this is a link system . this is a link system .
[staff_id] => 1
[staff_name] => System Administrator
[headers] =>
[source] => staff
[ip_address] => 10.12.145.174
[created] => 2016-09-27 01:49:27
[attachments] => 0
)

[1] => Array
(
[msg_id] => 7562
[ticket_id] => 1768
[messageId] =>
[msg_type] => R
[message] => Last message is this.
[staff_id] => 1
[staff_name] => System Administrator
[headers] =>
[source] => staff
[ip_address] => 10.12.145.174
[created] => 2016-09-26 08:39:46
[attachments] => 0
)

[2] => Array
(
[msg_id] => 7561
[ticket_id] => 1768
[messageId] =>
[msg_type] => R
[message] => Last message is this.
[staff_id] => 1
[staff_name] => System Administrator
[headers] =>
[source] => staff
[ip_address] => 10.12.145.174
[created] => 2016-09-26 08:37:25
[attachments] => 0
)

[3] => Array
(
[msg_id] => 7558
[ticket_id] => 1768
[messageId] =>
[msg_type] => R
[message] => mmmmmmmmmmmmmmmmmmmmmmmmmmm
[staff_id] => 1
[staff_name] => System Administrator
[headers] =>
[source] => staff
[ip_address] => 10.12.145.174
[created] => 2016-09-26 07:47:51
[attachments] => 0
)
)

然后我按以下代码对 $merge 数组的日期进行降序排序。

$strdate  = array();
$formated = array();
foreach ($merge as $ascdate){
$strdate[] = strtotime($ascdate['created']);
}
rsort($strdate);
foreach($strdate as $descdate){
$formated[] = date('Y-m-d H:i:s',$descdate);
}

现在我想代表日期对整个 $merge 数组进行降序排序。意味着最大的日期 $merge 数组应该排在最前面。我不知道如何在 $merge 数组中应用上述排序日期 $formated

最佳答案

使用usort对整个数组进行排序,而不仅仅是提取的日期

usort($merge,function ($a,$b) {
return $a['created'] < $b['created'];
});

关于php - 如何根据php中的日期列对mysql结果数组进行降序排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39738972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com