I have two associative array which have many content same and so I want to combine those two arrays in such a way that if I have a
in array 1 and a
$array1 + $array2
will return the union of the two arrays. If they are associative, values from the left operand will be preferred if keys collide. Therefore, this is not the solution you want as values will be lost.
There is nothing wrong with the statement provided both variables are understood as arrays. Your fatal error likely occurred because one variable was actually an object, and it's possible to mistake that using var_dump. By adding the type casts you forced PHP to coerce both variables to arrays. If one variable was an object, it would have this effect:
From the PHP.net Manual
"If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side."
Now having two arrays to add, PHP had nothing to panic about. Note that while casting the two operands of +
was important, it was meaningless to cast the variable you were assigning to. The expression (array)$foo
does not modify $foo, but rather returns a new value. This may not be intuitive from languages that require you to declare variables, but PHP is not such a language.
On your other issue, you cannot have two of the same key in an array. This would make it impossible to index the array as the correct value to return would become ambiguous. If you wish to combine two arrays in a lossless manor, you will have to use a more complicated data structure.
My suggestion is to use an array of arrays, where:
$a1 = array('a' => 1, 'b' => 2, 'c' => 3);
$a2 = array('a' => 3, 'b' => 2, 'd' => 2);
Will become:
$a3 = array(
'a' => array(1, 3),
'b' => array(2, 2),
'c' => array(3),
'd' => array(2)
);
And the ordering is not determined. The only notable change to the structure is that all first values are arrays, allowing values of duplicate keys to be accumulated. This function does the task and might do with a better name:
// array(array) lossless_array_merge([$array1 [, $array2 [, $...]]])
function lossless_array_merge() {
$arrays = func_get_args();
$data = array();
foreach ($arrays as $a) {
foreach ($a as $k => $v) {
$data[$k][] = $v;
}
}
return $data;
}