説明多次元配列の複数レベルのキーを移動するにはどうすればよいですか?
私が会話してアレイを持って、それぞれの会話は、一つまたは複数のメッセージが含まれていてもよいです。メッセージには1つまたは複数の添付ファイルが含まれ、添付ファイルは会話にバインドされます。私の目標は、添付ファイルを対応するメッセージに移動することです。ここで疑似配列だ:
$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
することにより、対応するメッセージの添付ファイルをバインドしたいと思い、各会話をループしたいと思います。どのようにこれを行うのですか?
期待される結果
$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'
]
]
]
]
]
];