설명다차원 배열의 여러 수준에서 키를 이동하려면 어떻게해야합니까?
나는 대화와 배열을 가지고, 각 대화는 하나 또는 여러 개의 메시지를 포함 할 수있다. 메시지에는 하나 이상의 첨부 파일이 포함될 수 있으며 첨부 파일은 대화에 바인딩됩니다. 내 목표는 첨부 파일을 해당 메시지로 이동하는 것입니다. 여기에 의사의 배열이있다 :
$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'
]
]
]
]
]
];