[英]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.