开发者

PHP add value to existing array value

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?


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;


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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜