组2D数组数据通过列

问题描述 投票:0回答:2

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;

我理想地猜测,我想在其中创建一个新数组,其中键是用户形(发送者),然后是他们的消息,然后与时间连接的每个消息相关联。什么是最好的方法?

eDit

您可以从数组示例中看到,有重复的用户从userIdfrom = 241的元素中进行了重复。这意味着用户241已发送了两条消息,如果有3个用户,241个元素,则意味着他/她发送了3条消息。我的目标是将用户从数组中的密钥组合在一起,因此两组将其与该密钥相关联,因此我们都有所有消息(消息)和创建消息的时间(timecreated)。

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

已编辑:

经过一点思考,我会这样做:
$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']  
    );
}
php arrays multidimensional-array grouping
2个回答
2
投票
保留订购的数据,然后将所有内容像您想要的订购一样敲击。 this应该净到这样的数组:

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' => (
)

他们将有这样的数组:

$newArray = array() foreach($json as $j) { $array[$j['useridfrom']][] = array('time'=>$j['timecreated'], 'message'=>$j[message]); }
,但也许我理解你不正确。
    

2
投票
php中的Array_multisort使用:

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


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.