0

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
   }
devlin carnate
  • 8,309
  • 7
  • 48
  • 82

2 Answers2

1

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.

//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'];

  }
}
Kenny
  • 1,543
  • 9
  • 16
0

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;
        }
    }
}
Yang
  • 8,580
  • 8
  • 33
  • 58
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • is the purpose of "break" to end the looping once the first match is found? (there will only be one match because those fields are the key). i just want to make sure i understand. – devlin carnate Sep 23 '13 at 14:23
  • Yes, that's the purpose. I assumed the matches would be unique. But I made a mistake, it should just be `break`, not `break 2`, to end the inner loop but continue the outer one. – Barmar Sep 23 '13 at 16:13