2

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?

1
  • 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. Commented Apr 21, 2011 at 20:17

5 Answers 5

4

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

2 Comments

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"
As the answer to my question was 'no', I've gone with the suggestion from KC above.
3

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

Comments

1

"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 :)

Comments

1

You can just use the built in merge function for arrays.

array_merge(array1,array2,array3...);

Using your array names here is how you do it array_merge($array_1 , $array_1);

Comments

0

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.

1 Comment

This assumes, that both the corresponding keys really exists in both arrays.

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.