简体   繁体   English

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

[英]merge two multi dimensional arrays based on combination of elements from one array in 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. 我想将$ inv_new添加到$ inv_h中,其中StoreNumber = org_number,SupplierNumber = Supplier_number和InvoiceNumber = invoice_number我已经尝试了array_merge,但是我无法确定如何匹配两个数组中的键,以便将新元素添加到“键”匹配。

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
   }

May be best in this case to just assign values manually, since there are different naming conventions used for keys and array_unique may prevent the multiple null values from being assigned. 在这种情况下,最好手动手动分配值,因为键使用不同的命名约定,而array_unique可能会阻止分配多个空值。

//iterate over the main array
foreach ($inv_h as $hkey => $hval) {

  //build our if statement (nice and tidy)
  $store_match = ($hval['org_number'] == $inv_new[0]['StoreNumber']);
  $supplier_match = ($hval['supplier_number'] == $inv_new[0]['SupplierNumber']);
  $invoice_match = ($hval['invoice'] == $inv_new[0]['InvoiceNumber']);

  if ($store_match && $supplier_match && $invoice_match) {
    //assign array values manually
    //cant use array merge because we would end up with some duplicates
    //due to different naming conventions (org_number vs StoreNumber)
    $inv_h[$hkey]['ReasonCode'] = $inv_new[0]['ReasonCode'];
    $inv_h[$hkey]['ReasonNote'] = $inv_new[0]['ReasonNote'];
    $inv_h[$hkey]['ResolutionCode'] = $inv_new[0]['ResolutionCode'];

  }
}

Just use loops to match the corresponding elements and then use array_merge() on the elements to copy the fields. 只需使用循环来匹配相应的元素,然后在元素上使用array_merge()即可复制字段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM