1

What is the best way to add two associative arrays such that duplicate values are not over written : + or array_ merge ?

I was pretty sure that using + operator I can add two associative arrays such that duplicate values are not over written but this Answer is saying something different and so am not sure if it is really true.

I would really appreciate if you can share some lights on how can we add two associative arrays such that duplicate values are not over written.

Appreciate your time and responses.

Community
  • 1
  • 1
user260204
  • 151
  • 2
  • 3
  • 9

5 Answers5

3

An array can't have more than one key-value pair with the same key. So if you have:

$array1 = array(
  'foo' => 5,
  'bar' => 10,
  'baz' => 6
);

$array2 = array(
  'x' => 100,
  'y' => 200,
  'baz' => 30
);

If you combine these arrays, you only get to keep one of the values of the combined array. The methods you describe do two different things:

print_r(($array1 + $array2));

// Result:
// Array
// (
//     [foo] => 5
//     [bar] => 10
//     [baz] => 6
//     [x] => 100
//     [y] => 200
// )

print_r(array_merge($array1, $array2));

// Result:
// Array
// (
//     [foo] => 5
//     [bar] => 10
//     [baz] => 30
//     [x] => 100
//     [y] => 200
// )

So you really need to define what you want to happen when you combine the arrays.

UPDATE

Based on @davidosomething's answer, here's what happens if you do array_merge_recursive():

print_r(array_merge_recursive($array1, $array2));

// Result:
// Array
// (
//     [foo] => 5
//     [bar] => 10
//     [baz] => Array
//         (
//             [0] => 6
//             [1] => 30
//         )
// 
//     [x] => 100
//     [y] => 200
// )
artlung
  • 33,305
  • 16
  • 69
  • 121
  • So in both the cases, I will lose one of the entities right, either it would be right side for array_merge or left side for `+` operator...In one response it was suggested to use array_merge_recursively, is this what would really solve the issue ? – user260204 Jan 28 '10 at 17:00
  • Thank you artlung, clear representation was really very very helpful to get details about how would it work. – user260204 Jan 28 '10 at 17:03
  • Does one supercede the other, or do they need to be combined somehow? I don't have a clear picture of what you're trying to do with the combined array. – artlung Jan 28 '10 at 17:03
  • It does not have to supercede the other but I should have two values than instead of one. Let say for example for offer id 1 there one product in Spain Catalog and one product in US Catalog but nwo when I display Complete Catalog than it should contain both the products which have been assigned key as offer id which is 1, hope this clarifies my scenario. – user260204 Jan 28 '10 at 17:13
  • Sounds like then, `array_merge_recursive` will work, so the key might be `in_catalog` and the values would be `array([0] => "Spain Catalog",[1] => "US Catalog")` - and you can display these as appropriate. Hope these answers helped you! I encourage you to learn more and participate on stackoverflow! – artlung Jan 28 '10 at 17:21
1

You actually want array_merge_recursive This creates an array of arrays if the KEY is the same but the value is different

Both array_merge and union will DISCARD one of the VALUES if a duplicate key is found

davidosomething
  • 3,379
  • 1
  • 26
  • 33
1

If you want to keep both values, you have to change the key for at least one of them. Perhaps you can write your own method to merge two arrays which prefixes all keys.

Thomas Müller
  • 1,433
  • 11
  • 24
0

array_merge will combine the arrays such that no values are lost, provided the arrays have contiguous numeric keys. If you start mixing string keys, values with the same keys will be overwritten. If you treat your arrays as arrays and not maps, array_merge will do what you want.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Array merge would not work in my case as I have strings keys and so if I have duplicate keys than values get over written and so this is not what I am looking for. – user260204 Jan 28 '10 at 16:52
0

a picture is better than 1000 words

$a = array('foo' => 'A');
$b = array('foo' => 'B');

print_r($a + $b);              // foo=A
print_r(array_merge($a, $b));  // foo=B
user187291
  • 53,363
  • 19
  • 95
  • 127
  • So in both the cases I am losing out on the one value where as what I am looking for is to get both the values, A and B for foo, is there any way to get this ? – user260204 Jan 28 '10 at 16:56