dqu3974 2017-06-12 09:53
浏览 32
已采纳

如果值在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条回答 默认 最新

  • dongpangzan6425 2017-06-12 10:31
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程