2017-06-12 226 views
-1

我有兩個數組,我想匹配第一個數組的鍵與另一個數組,如果兩個鍵匹配的值然後將第二個數組的值添加到數組1匹配數組值併合並,如果值匹配在PHP中

陣列1

[{"currency":1,"amount":23},{"currency":1,"amount":30},{"currency":2,"amount":40},] 

陣列2

[{"currency_id": 1,"currency_symbol":$},{"currency_id":2,"currency_symbol":€}] 

所需的輸出是:

[{"currency":$,"amount":23},{"currency":$,"amount":30},{"currency":€,"amount":40}] 

我使用的代碼是:

foreach($a1 as $key) { 
      foreach($a2 as $cKey){ 
       if($a1['currency']==$a2['currency_id']){ 
        $a1['currency_symbol'] = $a2['currency_symbol']; 
        echo $a1['currency_symbol']; 
       } 
      } 
     } 
+0

你的問題是什麼? –

+0

到目前爲止,您已經實現了哪些目標來實現所需的輸出? –

+0

我試着用foreach循環,但我不知道要與其他數組匹配鍵值, – mohit

回答

1
foreach($arr1 as $k=>$key) { 
    foreach($arr2 as $cKey){ 
     if($key['currency']==$cKey['currency_id']){ 
      $arr3[$k]['currency'] = $cKey['currency_symbol']; 
      $arr3[$k]['amount'] = $key['amount']; 
     } 
    } 
} 
var_export($arr3); 

你只需要分配變量和打印外循環。