如何在多维数组深层移动多维数组中的键?

时间:2017-11-03 08:18:11

标签: php arrays multidimensional-array

解释

我有一个包含对话的数组,每个对话可能包含一条或多条消息。消息可能包含一个或多个附件,附件绑定到会话。我的目标是将附件移动到相应的消息。这是伪数组:

tooltips: {
   mode: 'nearest'
}

我想遍历每个会话,如果设置了附件密钥,我想通过$conversations = [ [ 'id' => 'c1', 'messages' => [ [ 'id' => 'm1', 'content' => 'Herewith the attachments' ], [ 'id' => 'm2', 'content' => 'Ah, thanks' ], [ 'id' => 'm3', 'content' => 'What about the invoice?' ], [ 'id' => 'm4', 'content' => 'Oh shoot, here it is' ] ], 'attachments' => [ [ 'id' => 'a1', 'message_id' => 'm1', 'filename' => 'something.pdf' ], [ 'id' => 'a2', 'message_id' => 'm1', 'filename' => 'somethingelse.pdf' ], [ 'id' => 'a3', 'message_id' => 'm4', 'filename' => 'invoice.pdf' ] ] ] ]; 将附件绑定到相应的消息。怎么做到这一点?

预期结果

message_id

2 个答案:

答案 0 :(得分:0)

我会做这样的事情: 首先将id设置为数组的键,然后将附件添加到该键。

$joint_array = array();

foreach($conversations['messages'] as $x){
    $joint_array[$x['id']] = $x;
}

foreach($conversations['attachments'] as $y){
    $joint_array[$y['message_id']]['attachments'][] = $y;
}

答案 1 :(得分:0)

首先,我会将键更改为ID(id应该是唯一的,对吗?)因此数组中的项目可以稍微访问。然后将任何东西移动到任何东西应该是简单的,并且无需迭代即可访问它。

foreach($conversations AS $conversation) {
    $indexedMessages = [];

    foreach($conversation['messages'] AS $message) {
        $indexedMessages[$message['id']] = $message;
    }

    foreach($conversation['attachments'] AS $attachment) {
        $indexedMessages[$attachment['message_id']]['attachments'][/* you may put $attachment['id'] here */] = $attachment;
    }

    $result = [
        'id' => $conversation['id'],
        'messages' => $indexedMessages
    ];
}

$结果是这样的:

Array
(
    [id] => c1
    [messages] => Array
        (
            [m1] => Array
                (
                    [id] => m1
                    [content] => Herewith the attachments
                    [attachments] => Array
                        (
                            [0] => Array
                                (
                                    [id] => a1
                                    [message_id] => m1
                                    [filename] => something.pdf
                                )

                            [1] => Array
                                (
                                    [id] => a2
                                    [message_id] => m1
                                    [filename] => somethingelse.pdf
                                )

                        )

                )

            [m2] => Array
                (
                    [id] => m2
                    [content] => Ah, thanks
                )

            [m3] => Array
                (
                    [id] => m3
                    [content] => What about the invoice?
                )

            [m4] => Array
                (
                    [id] => m4
                    [content] => Oh shoot, here it is
                    [attachments] => Array
                        (
                            [0] => Array
                                (
                                    [id] => a3
                                    [message_id] => m4
                                    [filename] => invoice.pdf
                                )

                        )

                )

        )

)