Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I currently have two arrays, the key in each is a product id, whilst the value is the quantity:

$array_1 = ([10] => 1, [13] => 3, [27] => 1, [32] => 2);
$array_2 = ([8] => 1, [10] => 1, [12] => 1, [20] => 2, [27] => 1);

I want to combine the two array, but add the values together where the keys match. Is there a method to do this without iterating through the arrays?

share|improve this question
2  
In short, no. You'll have to iterate. PHP's internal merging functions only check for conflicts while merging, but will not DO anything with the values being merged. – Marc B Apr 21 '11 at 20:17

4 Answers

up vote 3 down vote accepted

The following is my belief on the shortest way to merge the two arrays together. There may be a built in function that I don't know about, but it will merge both arrays into $array_1.

foreach($array_1 as $k => $v)
    foreach($array_2 as $i => $j)
        if($k == $i) {
            $array_1[$k] = $v + $j;
            break;
        }

EDIT- break when found.

EDIT-(Thanks to KC)

foreach($array_1 as $k => $v)
    if(isset($array_2[$k])) {
        $array_1[$k] += $array_2[$k];
        unset($array_2[$k]);
    }

foreach($array_2 as $k => $v)
    $array_1[$k] = $v;
share|improve this answer
1  
n*m in worst case? Whats about just foreach($array_1 as $k => $v) { if (isset($array_2[$key])) { /* add */ } else { /* not add */ }? Feels much more efficient. You can unset the keys in $array_2, that already found. The remaining $array_2 are "untouched keys" – KingCrunch Apr 21 '11 at 20:29
As the answer to my question was 'no', I've gone with the suggestion from KC above. – bland_dan Apr 21 '11 at 20:44
I sit corrected :) – watcher Apr 21 '11 at 21:06

"Without iterating" no. Even if you wrap them into functions such as array_map its an iteration (its only not visible in the code itself ;))

$result = array_merge_recursive($array_1, $array_2);

On unique key, this will leave it, as it is. On two identical keys, it will merge the both values together into an array. A nice solution in PHP5.3

$result = array_map (function ($item) {
   return is_array($item) 
     ? $item[0] + $item[1]
     : $item;
 }, $result);

The non-5.3 solution is not that ugly too

foreach ($result as &$item) {
  if (is_array($item)) $item = $item[0] + $item[1];
}

Update:

If I read your question now, I doubt, that I understand what you mean by "add the values together". It makes not much sense to really add a product ID and a quantity together. However, just replace the + above with the operation of your needs :)

share|improve this answer

PHPs native array functions provide a few ways to split up and group the arrays, but an addition isn't possible (array_sum doesn't really help).

So while we're at recommending the solution that you specifically not asked for, here's a shorter one:

foreach ($array_2 as $i=>$k) {
    $array_1[$i] += $k;
}
share|improve this answer

This is the Function:

<?php
    function merge($array1, $array2)
    {

        if(sizeof($array1)>sizeof($array2))
        {
            echo $size = sizeof($array1);
        }else{
            $a = $array1;
            $array1 = $array2;
            $array2 = $a;

            echo $size = sizeof($array1);
        }

        $keys2 = array_keys($array2);

        for($i = 0;$i<$size;$i++)
        {
            $array1[$keys2[$i]] = $array1[$keys2[$i]] + $array2[$keys2[$i]];
        }

        $array1 = array_filter($array1);
        return $array1;
    }

?>

Taken From Here
and seems to me the most perfect solution.

share|improve this answer
This assumes, that both the corresponding keys really exists in both arrays. – KingCrunch Apr 21 '11 at 20:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.