我有两个数组我希望将第一个数组的键与另一个数组匹配,如果两个键的值匹配,则将第二个数组的值添加到数组1
阵列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 :(得分: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);
您只需要分配变量并打印外部循环。