1

我目前正在尝试在我的系统中实现消息传递功能。我现在的目标是有一个 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 元素,其中 useridfrom = 241 两次。这意味着用户 241 已经发送了两条消息,如果 241 个元素中有 3 个 userid,那么这意味着他/她发送了 3 条消息。我的目标是将两个用户标识组合在一起作为数组中的键,因此没有重复项,然后与该键相关联,我们拥有所有消息(消息)和创建消息的时间(创建时间)。

 [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"
  }

因此,从这个例子中,它应该导致一系列类似 mariomario 所建议的内容:

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"),
    )
}
4

3 回答 3

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),
    )
}

但也许我理解你不正确。

于 2012-05-09T12:28:05.277 回答
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' => (
)
于 2012-05-09T13:12:28.043 回答
0

在 PHP 中使用 array_multisort:

http://php.net/manual/en/function.array-multisort.php

于 2012-05-09T12:24:21.720 回答