2017-11-03 1 views
0

설명다차원 배열의 여러 수준에서 키를 이동하려면 어떻게해야합니까?

나는 대화와 배열을 가지고, 각 대화는 하나 또는 여러 개의 메시지를 포함 할 수있다. 메시지에는 하나 이상의 첨부 파일이 포함될 수 있으며 첨부 파일은 대화에 바인딩됩니다. 내 목표는 첨부 파일을 해당 메시지로 이동하는 것입니다. 여기에 의사의 배열이있다 :

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

답변

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

우선, 키를 ids (고유해야합니다. 맞습니까?)로 변경하면 배열의 항목에 어느 정도 액세스 할 수 있습니다. 그런 다음 무엇이든 움직이는 것은 반복적 인 일없이 간단하게 액세스해야합니다.

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 
           ) 

         ) 

       ) 

     ) 

)
:

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

$ 결과는 이것이다

관련 문제