简体   繁体   English

如何将多维数组中的键移动到多个级别?

[英]How does one move a key in a multidimensional Array multiple levels deep?

Explanation 说明

I have an Array with conversations, each conversation may contain one or multiple messages. 我有一个包含对话的数组,每个对话可能包含一个或多个消息。 A message may contain one or multiple attachments, the attachments are bound to a conversation. 邮件可能包含一个或多个附件,附件绑定到对话。 My goal is to move the attachment(s) to the corresponding message. 我的目标是将附件移到相应的消息。 Here's the pseudo Array: 这是伪数组:

$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'
                ]
            ]
        ]
    ];

I would like to loop through each conversation, if the attachments key is set I would like to bind the attachment to the corresponding message by message_id . 我想遍历每个对话,如果设置了附件密钥,我想通过message_id将附件绑定到相应的消息。 How does one do this? 如何做到这一点?

Expected result 预期结果

$conversations = [
        [
            'id' => 'c1',
            'messages' => [
                [
                    'id' => 'm1',
                    'content' => 'Herewith the attachments',
                    'attachments' => [
                        [
                            'id' => 'a1',
                            'message_id' => 'm1',
                            'filename' => 'something.pdf'
                        ],
                        [
                            'id' => 'a2',
                            'message_id' => 'm1',
                            'filename' => 'somethingelse.pdf'
                        ]
                    ]
                ],
                [
                    'id' => 'm2',
                    'content' => 'Ah, thanks'
                ],
                [
                    'id' => 'm3',
                    'content' => 'What about the invoice?'
                ],
                [
                    'id' => 'm4',
                    'content' => 'Oh shoot, here it is',
                    'attachments' => [
                        [
                            'id' => 'a3',
                            'message_id' => 'm4',
                            'filename' => 'invoice.pdf'
                        ]
                    ]
                ]
            ]
        ]
    ];

I would do something like this: Start with setting the id as key of the array, then add the attachments to that key. 我将执行以下操作:首先将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;
}

First of all, I would change keys to ids (id should be unique, right?) so items in array will be somewhat accessible. 首先,我将键更改为id(id应该是唯一的,对吗?),以便可以稍微访问数组中的项。 Then moving anything to anything should be simple as well as accessing it without iteration. 然后,将任何内容移动到任何内容都应该很简单,并且无需迭代即可访问它。

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
    ];
}

$result is this: $ result是这样的:

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
                                )

                        )

                )

        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按键对多维数组的所有级别进行排序? - How to sort all levels of multidimensional array by key? 如何使用数据提供程序测试在多维数组的不同级别上不存在键? - How can I test that a key does NOT exist at different levels of a multidimensional array using data providers? php 按子键值对多层深的多维数组进行排序 - php sort multidimensional array that is multiple layers deep by sub key value 如何使用键从多维数组检索深子数组? - How to retrieve a deep subarray using key from a multidimensional array? 如何使用键值在多维数组中移动数组? - How to move array in multidimensional array using key value? 如何为多个键订购多维数组? - How order a multidimensional array for multiple key? 按键将平面数组分组为多维数组,如果我们不知道有多少级别。 PHP - Group flat array to multidimensional array by key, If we don't know how many levels are there. PHP 如何检查多维数组在所有“级别”上是否都包含某些键和值对? - How to check if the multidimensional array contains certain key and value pair on all “levels”? 如何在PHP中按一个可能在三个级别的键对多维哈希数组进行排序? - How do I sort a multidimensional hash array by a key maybe three levels in, in PHP? 如何获取多维数组中不包括一个键的数组的一部分? - How to get part of array excluding one key in multidimensional array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM