Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP for loop logic (having a mental block)

Tags:

php

for-loop

I have 2 arrays

$companyA[] = array ("QuantityOnOrder" => $quantityOnOrder, "QuantityOnHand" => $quantityOnHand, "Name" => $name);
$companyB[] = array ("QuantityOnOrder" => $quantityOnOrder, "QuantityOnHand" => $quantityOnHand, "Name" => $name);

These are multi-dimensional arrays. Let's say that company A has 3 records and company B has 10. Let's further assume that 1 of the records in companyA share the same "Name" property as a record in companyB. If that is true, then I want to take the quantityOnOrder and quantityOnHand from each array and add their values and store them in a new array.

I then want to also make sure that this new array has all of the unique values from both arrays as well. Here's an example of what the arrays may look like and what I would like the end result to look like.

CompanyA:

Array ( 
[0] => Array ([QuantityOnHand] => 10 [QuantityOnOrder] => 20 [Name] => LOR1) 
[1] => Array ([QuantityOnHand] => 5 [QuantityOnOrder] => 6 [Name] => D23-72-P) 
[2] => Array ([QuantityOnHand] => 2331 [QuantityOnOrder] => 0 [Name] => RB) 
[3] => Array ([QuantityOnHand] => 3520 [QuantityOnOrder] => 0 [Name] => RTOP))

CompanyB:

Array ( 
    [0] => Array ([QuantityOnHand] => 11 [QuantityOnOrder] => 13 [Name] => RPEN) 
    [1] => Array ([QuantityOnHand] => 5 [QuantityOnOrder] => 6 [Name] => D23-72-P) 
    [2] => Array ([QuantityOnHand] => 23 [QuantityOnOrder] => 0 [Name] => RAT) 
    [3] => Array ([QuantityOnHand] => 320 [QuantityOnOrder] => 0 [Name] => RBOT))

CombinedArray:

Array ( 
    [0] => Array ([QuantityOnHand] => 10 [QuantityOnOrder] => 20 [Name] => LOR1) 
    [1] => Array ([QuantityOnHand] => 10 [QuantityOnOrder] => 12 [Name] => D23-72-P) 
    [2] => Array ([QuantityOnHand] => 2331 [QuantityOnOrder] => 0 [Name] => RB) 
    [3] => Array ([QuantityOnHand] => 3520 [QuantityOnOrder] => 0 [Name] => RTOP))
    [4] => Array ([QuantityOnHand] => 11 [QuantityOnOrder] => 13 [Name] => RPEN) 
    [5] => Array ([QuantityOnHand] => 23 [QuantityOnOrder] => 0 [Name] => RAT) 
    [6] => Array ([QuantityOnHand] => 320 [QuantityOnOrder] => 0 [Name] => RBOT))

Notice how in the combined array I have all of the records from companyA and companyB and the records that share the same name have been combined. Hope this makes enough sense to someone. I can't figure out the best way to go about achieving the results that I'm looking for in the combined array. Any help would be much appreciated.

like image 259
chrs911 Avatar asked Mar 03 '26 23:03

chrs911


1 Answers

I think this might be an easier go if this was a associative array with the key being the name and the value the array.

Otherwise, I would index the array first to prevent more looping then necessary. This is untested but maybe you could give something like this a go.

$i = 0;
foreach($company_a as $a) {
    $idx_a[$a['Name']] = $i++;
}

$i = 0;
foreach($company_a as $b) {
    $idx_b[$b['Name']] = $i++;
}

$toMerge = array_filter($company_b, function($value) {
    return array_key_exists($value['Name'], $idx_a);
});
$toRemove = array();
foreach($toMerge as $m) {
    $i = $idx_a[$m['Name']];
    $c = $company_a[$i];
    $company_a[$i]['QuantityOnHand'] = $c['QuantityOnHand'] + $m['QuantityOnHand'];
    $company_a[$i]['QuantityOnOrder'] = $c['QuantityOnOrder'] + $m['QuantityOnOrder'];
    $toRemove[] = $idx_b[$m['Name']];
}

foreach($toRemove as $i) {
    unset($company_b[$]);
}

//company_a now has the final result
$company_a = array_merge($company_a, $company_b);
like image 168
Paul Zepernick Avatar answered Mar 05 '26 13:03

Paul Zepernick