Home » Web Design and Development » PHP » Merging two arrays in php using array_merge function and creating merged array with unique elements

Merging two arrays in php using array_merge function and creating merged array with unique elements

If you are completely new to an arrays in PHP, read our post “How to create Arrays in php ?”

Now, lets assume, you are aware of basics of array as mentioned above. We will create two arrays as below,

$testArrayForMerging = array();
$testArrayForMerging[0] = 25;
$testArrayForMerging[1] = 55;
$testArrayForMerging[2] = 78;
$testArrayForMerging[3] = 7;
$testArrayForMerging[4] = 3;

for ($i = 0; $i < 10; $i++) {
	$arrayUsingForLoop[] = $i;
}

Above code creates two arrays and initializes those to some values. These two arrays can be simple merged using “array_merge” API as below,

$array_after_merging = array_merge($arrayUsingForLoop,$testArrayForMerging);
print_r($array_after_merging);

Here, note that we passed names of these two arrays and created third new array named “array_after_merging” and displayed the array using “print_r”. “array_merge” Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

The output after merging will look like as below,

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 25 [11] => 55 [12] => 78 [13] => 7 [14] => 3 )  

Note here, that the second array has been appended as is to first array, and array_merge preserved the index of the arrays to display.

Creating array with unique values in elements

$unique_array_after_merging = array_unique($array_after_merging);
print_r($unique_array_after_merging);

The output to this will be as below,

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 25 [11] => 55 [12] => 78 )

NOTE: here the final array will have only 12 elements, removing the duplicated 7 & 3 from element [13] and [14] from previously merged array

Using only values from the merged arrays / merging arrays without keys

As we seen above array_merge preserves and shows the keys / index’s of the array elements, but sometimes we only need values from the merged array, for this there is no ready “merge without keys” function, so we will create another new array from the merged array and use function “array_values” for getting only elements in new array as,

$merged_array_without_keys = array_values($unique_array_after_merging);

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment