我不是英语本地人,并且在stackoverflow上是新的,所以如果我做了一些事情,请纠正我。 我的问题是我从两个不同的mysql数据库名表中检索数据。
- 事件
- 消息
醇>
用于获取数据的代码如下。
$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
数组应该排在最前面。我不知道如何在$formated
数组中应用以上排序日期$merge
。
答案 0 :(得分:1)
这样做,这是你在寻找合并两个数组之后,它使用数组多重排序 -
function array_orderby()
{
$args = func_get_args();
$data = array_shift($args);
foreach ($args as $n => $field) {
if (is_string($field)) {
$tmp = array();
foreach ($data as $key => $row)
$tmp[$key] = $row[$field];
$args[$n] = $tmp;
}
}
$args[] = &$data;
call_user_func_array('array_multisort', $args);
return array_pop($args);
}
$arr = array(
array('msg_id'=>123,'ticket_id'=>'32424','date'=>1233455,'created' => '2016-09-26 07:47:51'),
array('msg_id'=>143,'ticket_id'=>'sssds','date'=>44724,'created' => '2016-09-27 07:47:51'),
array('msg_id'=>13,'ticket_id'=>'3xxxx','date'=>434442424,'created' => '2016-10-26 07:47:51'),
array('msg_id'=>723,'ticket_id'=>'eeeeeee','date'=>9908,'created' => '2016-06-06 17:47:51'),
array('msg_id'=>793,'ticket_id'=>'fffff','date'=>9908,'created' => '2016-06-06 17:47:56')
);
echo "Before Sorting";
print_r($arr);
$arr = array_orderby($arr, 'created', SORT_DESC) ;
echo "After sorting";
print_r($arr);
只需运行此示例即可获得所需内容
答案 1 :(得分:0)
使用usort对整个数组进行排序,而不仅仅是提取日期
first
答案 2 :(得分:0)
您可以为此
使用多重排序API参考:示例#3对数据库结果进行排序
http://php.net/manual/en/function.array-multisort.php
<?php
$aMultiSort = array();
$aTimeStamp = array();
foreach ($merge as $key=>$ascdate){
//create strtotime for numerical sorting
$ascdate['timestamp'] = strtotime($ascdate['created']);
$aMultiSort[$key] = $ascdate;
$aTimeStamp[$key] = $ascdate['timestamp'];
}
//multi sort with timestamp desc
array_multisort($aTimeStamp, SORT_DESC, $aMultiSort);
答案 3 :(得分:0)
在这种情况下,我可能会创建一个新方法,我称之为getAllComments()
(或类似的东西)。在这个方法中,我可能会使用JOIN
或UNION
查询,同时从两个表/行(?)中选择所有信息。在此查询中,按日期排序非常简单,特别是如果它是JOIN
查询。
如果是UNION
查询,您可以将其包装在外部SELECT中,以便对数据进行排序。像这样:
SELECT * FROM (
SELECT id, text, date FROM messages
UNION SELECT id, text, date FROM notes
) ORDER BY date
无需涉及(相对)昂贵的PHP排序,也无需编写和测试代码所涉及的所有额外工作。毕竟,SQL引擎专门用于这些操作。 :)