2017-11-03 12 views
0

説明多次元配列の複数レベルのキーを移動するにはどうすればよいですか?

私が会話してアレイを持って、それぞれの会話は、一つまたは複数のメッセージが含まれていてもよいです。メッセージには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' 
         ] 
        ] 
       ] 
      ] 
     ] 
    ]; 

答えて

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

まず配列のアイテムはややアクセスできるようになりますので、私は(idは右、一意である必要があります?)IDにキーを変更します。何かを何かに動かすことは、反復せずにアクセスするだけでなく、単純でなければなりません。

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

$結果がこれです:

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 
           ) 

         ) 

       ) 

     ) 

)
関連する問題