如果值在PHP中匹配,则匹配数组值并合并[关闭]

如果值在PHP中匹配,则匹配数组值并合并[关闭]

问题描述:

I have two array i want to match a key of first array with another array and if the values of both key matches then add the value of second array to array 1

Array 1

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

Array 2

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

Desired output is:

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

The Code i am using is:

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

我有两个数组我希望将第一个数组的键与另一个数组匹配,如果两个键的值匹配 然后将第二个数组的值添加到数组1 p>

数组1 p>

  [{“currency”:1,“amount”:23  },{“currency”:1,“amount”:30},{“currency”:2,“amount”:40},] 
  code>  pre> 
 
 

数组2 p>

  [{“currency_id”:1,“currency_symbol”:$},{“currency_id”:2,“currency_symbol”:€}] 
  code>  
 
 

所需的输出是: p>

  [{“currency”:$,“amount”:23},{“currency”:$,  “金额”:30},{“货币”:€,“金额”:40}] 
  code>  pre> 
 
 

我使用的代码是: p> \ n

  foreach($ a1 as $ key){
 foreach($ a2 as $ cKey){
 if($ a1 ['currency'] == $ a2 ['currency_id'])  {
 $ a1 ['currency_symbol'] = $ a2 ['currency_symbol']; 
 echo $ a1 ['currency_symbol']; 
} 
} 
} 
  code>  pre> \  n  div>

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);

You only need to assign variable and print outside loop.