User Contributed Notes 64 notes

In some situations, the union operator ( + ) might be more useful to you than array_merge. The array_merge function does not preserve numeric key values. If you need to preserve the numeric keys, then using + will do that.

ie:

<?php

$array1[0] = "zero";$array1[1] = "one";

$array2[1] = "one";$array2[2] = "two";$array2[3] = "three";

$array3 = $array1 + $array2;

//This will result in::

$array3 = array(0=>"zero", 1=>"one", 2=>"two", 3=>"three");

?>

Note the implicit "array_unique" that gets applied as well. In some situations where your numeric keys matter, this behaviour could be useful, and better than array_merge.

An addition to what Julian Egelstaff above wrote - the array union operation (+) is not doing an array_unique - it will just not use the keys that are already defined in the left array. The difference between union and merge can be seen in an example like this:

to get unique value from multi dimensional array use this instead of array_unique(), because array_unique() does not work on multidimensional:array_map("unserialize", array_unique(array_map("serialize", $array)));Hope this will help someone;Example$a=array(array('1'),array('2'),array('3'),array('4));$b=array(array('2'),array('4'),array('6'),array('8));$c=array_merge($a,$b);then write this line to get unique values$c=array_map("unserialize", array_unique(array_map("serialize", $c)));print_r($c);

array_merge is the equivalent of concat in other languages. I tried to submit a bug that array_concat should be created as an alias to this but it was rejected on the basis they didn't want to polute the namespace and that the documentation should be updated instead. So here's what I put in the bug #73576:

There is no documented array concatenation function. This is a very common function, e.g. Javascript and Ruby have the `concat` function, Python has `+` and Haskell has `++`.

The `array_merge` function is what has be used if you want to concatenate arrays. However it is not mentioned in the documentation (not even in the comments) of that method that that is what should be used.

I propose that `array_concat` be created as an alias of `array_merge`. The concatenation of an associative array is also consistent with trying to merge the hash maps. For example there is a Stack Overflow question on 'concatenating two dictionaries' that is marked as a duplicate of the function 'How to merge two Python dictionaries'. That is, it is consistent that hash map concatenation is the same as hash map merging.

So I believe that `array_concat` is a perfect alias for `array_merge` in terms of numeric arrays and a valid (albeit unnecessary) alias for associative arrays.

This will help almost all developers coming to PHP from other dynamic languages.

The documentation is a touch misleading when it says: "If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way." Even with two arrays, the resulting array is re-indexed:

More on the union (+) operator:
the order of arrays is important and does not agree in my test below
with the other posts. The 'unique' operation preserves the initial key-value and discards later duplicates.

It is not officially documented but it is summarily important information for everyone to know: neither array_merge or array_merge_recursive functions will function correctly if non-array objects are used as parameters.

You would probably expect these functions to ignore non-array elements, right? However, if one parameter is null it PHP will not only return null for the entire function but will also (not always) raise an error, such as :

[ Warning: array_merge(): Argument #x is not an array... ]

This error message won't appear if the defective variable is an empty array (an empty array is still an array), but it will result in an undesirable incomplete Array.

There are several solutions for this problem by validating the Arrays before use them in these functions, but the most efficient way is to enforce the element as an array directly in the function itself. I.e.:

The problem that the array keys are reindexed, and further not working on array values of type object or other mixed types.

Simply, I did this:

<?php
foreach($new as $k => $v)
{
$old[$k] = $v;
}
// This will overwrite all values in OLD with any existing
// matching value in NEW
// And keep all non matching values from OLD intact.
// No reindexing, and complete overwrite
/// Working with all kind of data
?>

I had a hard time using array_merge with large datasets. By the time my web framework was in memory there wasn't enough space to have multiple copies of my dataset. To fix this I had to remove any functions which operated on the data set and made a copy in memory (pass by value).

I realize the available memory to an application instance is modifiable, but I didn't think I should have to set it below the default 16mb for a web app!

Unfortunately, a number of php functions pass by value internally, so I had to write my own merge function. This passes by reference, utilizes a fast while loop (thus doesn't need to call count() to get an upper boundary, also a php pass by value culprit), and unsets the copy array (freeing memory as it goes).

An earlier comment mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice. Performing the following functions in a 100,000-iteration loop gave me the following times: ($b is a 3-element array)

I got tripped up for a few days when I tried to merge a (previously serialized) array into a object. If it doesn't make sense, think about it... To someone fairly new, it could... Anyway, here is what I did:(It's obviously not recursive, but easy to make that way)<?phpfunction array_object_merge(&$object, $array) { foreach ($array as $key => $value)$object->{$key} = $value;}?>

Be ready to surprise like this one.array_merge renumbers numeric keys even if key is as string.keys '1' & '2' will be updated to 0 & 1, but '1.1' & '1.2' remain the same, but they are numeric too (is_numeric('1.1') -> true)

It's better to use '+' operator or to have your own implementation for array_merge.

There was a previous note that alluded to this, but at least in version 5.2.4 array_merge will return NULL if any of the arguments are NULL. This bit me with $_POST when none of the controls were successful.