views:

113

answers:

6

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.

A: 

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.

meagar
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.
+1  A: 

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
SO will array_merge_recursive not discard one of the VALUES if a duplicate key is found ? Is this 100 % true ?
+1 as it seems to be what the question asker wanted
Yacoby
A: 

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
stereofrog
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 ?
array_merge_recursive is what you're looking for
stereofrog
+1  A: 

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
So you suggest that Suggestion Answer link provide is 100 % true ?
+2  A: 

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
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 ?
Thank you artlung, clear representation was really very very helpful to get details about how would it work.
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
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.
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
A: 

Arrays merged with + will have any keys of the left hand operand preserved, while array_merge will overwrite associative keys, but adds any numeric keys.

See my answer for an example

When you talk of values, do you mean keys or values, e.g. in 'foo' => 'bar', the value would be bar.

Gordon
Is there a way where I can preserve both the values right and left, will array_merge_recursive solve the issue ?
It won't. You can keep all values, but you will lose their association to the index. And string keys have to be unique, so you cannot have two keys *foo* on the same depth. If you update your question with your arrays and specify how you want them to look after merging, we might come up with a approximate solution though.
Gordon
I mean values as in bar for the example provided. I am in the process of updating my work and once it is ready I will put both associative array that I am looking to merge. Thanks for all help with this.