donglu1971 2013-09-20 22:55
浏览 73
已采纳

基于PHP中一个数组的元素组合合并两个多维数组

I have two arrays. I want to add $inv_new to $inv_h where StoreNumber = org_number, SupplierNumber = supplier_number and InvoiceNumber = invoice_number I have tried array_merge, but I can't determine how to match the keys in the two arrays so that the new elements are added where the "keys" match.

vdump($inv_new);
 array(1) {
   [0] => array(6) {
   'StoreNumber' → str•3 '11 '
   'SupplierNumber' → str•4 '6303'
   'InvoiceNumber' → str•11 'DI613718812'
   'ReasonCode' → str•4 'Dept'
   ["ReasonNote"] → NULL
   ["ResolutionCode"] → NULL
   }
 }
 vdump($inv_h);
 array(30) {
  ....
   [22] => array(5) {
   'org_id' → str•2 '11'
   'org_number' → str•2 '11'
   'supplier_number' → str•4 '6303'
   'supplier_name' → str•27 'BLAH'
   'invoice_number' → str•11 'DI613718812'
   }

would result in:

      array(30) {
  ....
   [22] => array(8) {
   'org_id' → str•2 '11'
   'org_number' → str•2 '11'
   'supplier_number' → str•4 '6303'
   'supplier_name' → str•27 'BLAH'
   'invoice_number' → str•11 'DI613718812'
   'ReasonCode' -> str 4 'Dept'
   ["ReasonNote"] -> NULL
   ["ResolutionCode"] -> NULL
   }
  • 写回答

2条回答 默认 最新

  • douzhanglun4482 2013-09-20 23:10
    关注

    Just use loops to match the corresponding elements and then use array_merge() on the elements to copy the fields.

    foreach ($inv_new as $inv1) {
        foreach ($inv_h as &$inv2) {
            if ($inv1['StoreNumber'] == $inv2['org_id']
                && $inv1['SupplierNumber'] == $inv2['supplier_number']
                && $inv1['InvoiceNumber'] == $inv2['invoice_number']) {
                $inv2 = array_merge($inv1, $inv2);
                break;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献