3

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.

4
  • Have you tried something ? Commented Apr 7, 2015 at 19:43
  • Yes I have but I think that the solution that I'm working on is taking me down the wrong path. I feel like I am overcomplicating it. Commented Apr 7, 2015 at 19:59
  • The show it to us! So we can show you where the problem is and how to fix it Commented Apr 7, 2015 at 19:59
  • I run 2 for loops the first for loop iterates over the companyA array gets the name and then i run a second for loop within the first one and check if the name is equal to the name of the first for loop. if it is i then add the value and add the results to a new array. if not then I add just the record from companyA. I don't know at which point I would add records from companyB though without constantly duplicating them Commented Apr 7, 2015 at 20:01

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was exactly what I needed. The only thing I had to add to make it work was to add use($idx_a) after function($value) so that the array_key_exists function would have access to the $idx_a array
1

Here is a concept that you can use, you will need to make it work because it's just the concept (not real code).

$array1 = array();
$array2 = array();
$array3 = array();
$array1.sortByName;
$array2.sortByName;

$array3 = setArray3($array1,$array2,$array3,$array3,0,0);

function setArray3($array1,$array2,$array3,$pointer1,$pointer2){
$count = count($array3);

if(isset($array1[$pointer1])){
    if(isset($array2[$pointer2])) {
        if ($array1[$pointer1]['name'] > $array2[$pointer2]['name']) {
            $array3[$count] = $array1[$pointer1];
            $pointer1++;
            } elseif ($array1[$pointer1]['name'] < $array2[$pointer2]['name']) {
                $array3[$count] = $array2[$pointer2];
                $pointer2++;
            } else {
                //add both the arrays togeather
                $row = $array1[$pointer1] + $array2[$pointer2];
                $array3[$count] = $row;
                $pointer1++;
                $pointer2++;
            }
            setArray3($array1,$array2,$array3,$pointer1,$pointer2);
        }else{
            // add the rest of array1 to array 3
        }
    }else{
    //add the rest of array 2 to array 3
    }
return $array3;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.