我目前正在尝试在我的系统中实现消息传递功能。我现在的目标是拥有一个jQuery弹出对话框,该对话框显示已向另一个人发送消息的用户名,然后单击该人的姓名将显示新消息。我已经完成了弹出对话框,所以我的下一步是从数据库获取和排序消息。
我现在所做的是从数据库中检索新消息。我不确定如何继续,因为我的未读消息数组不止一次包含发件人ID的元素。我不想循环遍历未读消息,然后一次又一次地获取相同的发送者ID,并因此一次又一次地从数据库中检索他们的详细信息。
这是我的数组的一个例子:
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[2]=>
array(7) {
["id"]=>
string(2) "10"
["useridfrom"]=>
string(3) "138"
["useridto"]=>
string(3) "139"
["message"]=>
string(54) "Hi Emma thank you for submitting your homework - Jenny"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1309122193"
["messagetype"]=>
string(6) "direct"
}
}
到目前为止,这是我在PHP中所做的:
$m = new Messaging();
$json = $m->getUnreadMessages($uid);
/** sort messages by useridfrom **/
uasort($json, array($m, 'messageCompare'));
foreach($json as $j)
{
//do sorting of messages here ??
}
echo json_encode($json);
exit;
我想理想情况下,我想创建一个新的数组,其中密钥是useridfrom(发送者),然后是他们的消息,然后与每个消息相关联的时间创建。做这个的最好方式是什么?
提前致谢。
修改
从数组示例中可以看出,有两个useridfrom = 241的useridfrom元素重复。这意味着用户241已经发送了两个消息,如果有来自241个元素的3个用户ID,那么这意味着他/她发送了3个消息。我的目标是将两个useridfrom组合在一起作为数组中的键,因此没有重复项然后与该键相关联,我们拥有所有消息(消息)和消息创建时间(时间创建)。
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
因此,从这个例子来看,它应该导致像马里奥马里奥建议的那样的数组:
array{
['241'] => array(
[0] => array(time=>'time1', message => "hello i am 241"),
[1] => array(time=>'time2', message => "nice to meet you"),
),
['330'] => array(
[0] => array(time=>'time1', message => "hello i am 330"),
[1] => array(time=>'time2', message => "goodbye"),
)
}
答案 0 :(得分:2)
你不会做这样的事情
$newArray = array()
foreach($json as $j)
{
$array[$j['useridfrom']][] = array('time'=>$j['timecreated'],
'message'=>$j[message]);
}
他们会有这样的数组:
array{
['user1'] => array(
[0] => array(time=>'time1', message => message1),
[1] => array(time=>'time2', message => message2),
),
['user2'] => array(
[0] => array(time=>'time1', message => message1),
[1] => array(time=>'time2', message => message2),
)
}
但也许我理解你不正确。
答案 1 :(得分:2)
编辑:
经过一番思考后,我会这样做:$data = array();
foreach ($json as $j) {
if (!array_key_exists($j['useridfrom'], $data)) {
$data[$j['useridfrom']] = array();
}
if (!array_key_exists($j['useridto'], $data[$j['useridfrom']])) {
$data[$j['useridfrom']][$j['useridto']] = array();
}
$data[$j['useridfrom']][$j['useridto']][] = array(
'message' => $j['message'],
'timestamp' => $j['timecreated']
);
}
按照ORDER BY timecreated DESC
的顺序保存您的数据,然后按照您想要的时间排序所有内容。
这应该是这样的一个数组:
'sender1' => (
'recipient1' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
),
(
'message' => 'ANOTHER MESSAGE HERE',
'timecreated' => 'ANOTHER TIME HERE'
)
),
'recipient2' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
)
)
),
'sender2' => (
)
答案 2 :(得分:0)
在PHP中使用array_multisort: