How to merge arrays with same key and different value in PHP?

11,998

Solution 1

// array array_merge_values($base[, $merge[, ...]])
// Combines multiple array values based on key
//   (adding them together instead of the native array_merge using append)
//
// $base       - array to start off with
// $merge[...] - additional array(s) to include (and add their values) on to the base
function array_merge_values()
{
  $args = func_get_args();

  $result = $args[0];
  for ($_ = 1; $_ < count($args); $_++)
    foreach ($args[$_] as $key => $value)
    {
      if (array_key_exists($key,$result))
        $result[$key] += $value;
      else
        $result[$key] = $value;
    }
  return $result;
}

$array1 = Array('foo' => 5, 'bar' => 10, 'foobar' => 15);
$array2 = Array('foo' => 20,                             'foohbah' => 25);
$array3 = Array(            'bar' => 30);
var_dump(array_merge_values($array1,$array2,$array3));

Result:

array(4) {
  ["foo"]=>
  int(25)
  ["bar"]=>
  int(40)
  ["foobar"]=>
  int(15)
  ["foohbah"]=>
  int(25)
} 

That what you're looking for?

Solution 2

This should work:

$outArray = array()
foreach($superArray as $subArray) {
  if(array_key_exists($outArray,$subArray[0])) { 
    $outArray[$subArray[0]] += $subArray[1]; 
  } else { 
    $outArray[$subArray[0]] = $subArray[1]; 
  }
}
Share:
11,998

Related videos on Youtube

Martin
Author by

Martin

Updated on June 04, 2022

Comments

  • Martin
    Martin almost 2 years

    I have arrays similarly to these:

    0 => Array ( [0] => Finance / Shopping / Food, [1] => 47 )            
    1 => Array ( [0] => Finance / Shopping / Food, [1] => 25 )                 
    2 => Array ( [0] => Finance / Shopping / Electronic, [1] => 190 ) 
    

    I need to create one array with [0] as a key and [1] as value.
    The tricky part is that if the [0] is same it add [1] to existing value.

    So the result I want is:

    array ([Finance / Shopping / Food]=> 72, [Finance / Shopping / Electronic] => 190);
    

    thanks

    • dqhendricks
      dqhendricks over 13 years
      it looks as though you should be using key/value pairs instead of two array elements, one being a key, and a second being a value. is there a reason you can't build the array in the first place like this? Array('Finance / Shopping / Food' => 47, 'Finance / Shopping / Food' => 25, 'Finance / Shopping / Electronic' => 190) This would make the rest of what you are trying to do far more simple.
    • dqhendricks
      dqhendricks over 13 years
      I'm not even entirely sure your syntax works as is. array keys can either be strings or numeric values, but yours appear to be constants divided by each other and other weird stuff. you may want to read up on php arrays here: php.net/manual/en/language.types.array.php
  • fredley
    fredley over 13 years
    In fact, it'll probably work without the if/else, just loop over the whole thing with $outArray[$subArray[0]] += $subArray[1].
  • Brad Christie
    Brad Christie over 13 years
    It will work, but not a good idea to perform a += on an uninitialized value (although I suppose in this case it's trivial, assuming 0 is the "base" value.)
  • fredley
    fredley over 13 years
    @Brad My thoughts exactly. My answer is more correct, but PHP is suitably flexible/dirty to allow the code in the comment :-)