2017-03-29 49 views
0

我有兩個數組,我想將它們組合到合併數組如何使用相同的ID

1)首先是這樣的:

[11] => Array 
    (
     [id] => 11 
     [name] => test 
    ) 
[12] => Array 
    (
     [id] => 12 
     [name] => test1 
    ) 

2)第二個數組是這樣的:

[0] => Array 
    (
     [offer_id] => 11 
     [countries] => Array 
      (
       [SA] => Array 
        (
         [id] => 682       
        ) 
      ) 
    ) 

[1] => Array 
    (
     [offer_id] => 12 
     [countries] => Array 
      (
       [KW] => Array 
        (
         [id] => 414       
        ) 
      ) 
    ) 

我想要這個結果。任何人如何爲同樣的解決方案提供解決方案?

[11] => Array 
    (
     [id] => 11 
     [name] => test 
     [countries] => Array 
      (
       [SA] => Array 
        (
         [id] => 682       

        ) 

      ) 

    ) 
[12] => Array 
    (
     [id] => 12 
     [name] => test 
     [countries] => Array 
      (
       [KW] => Array 
        (
         [id] => 414        
        ) 
      ) 
    ) 

謝謝你的幫助!

+0

請格式化PHP數組更新您的問題,並格式化你的預期輸出數組 –

回答

1

試試這個:

foreach ($array1 as &$arr1) { 
    $offer_id = $arr1['id']; // Search for this offer_id in array 2 
    $match = array_filter($array2, function($v) use ($offer_id){ 
     return $v['offer_id'] == $offer_id; // Return matching offer id 
    }); 
    $arr1['countries'] = current($match)['countries']; // Assign matched country to array 
} 
+0

非常感謝你這是工作....! –