Logo

How to Merge Two Arrays with Unique Values in PHP?

How to Merge Two Arrays with Unique Values in PHP?

How to Merge Two Arrays with Unique Values in PHP?

Hello, PHP enthusiasts! Merging arrays is a common task in PHP, but ensuring that the merged array contains only unique values can be a bit tricky.
In this article, we’ll explore several methods to merge two arrays and ensure the resulting array has only unique values. Let’s dive into the various techniques!

Method 1 : Using array_merge() and array_unique()

One straightforward way to merge two arrays with unique values is by combining array_merge() with array_unique().

Example 1 : Merging Arrays with array_merge() and array_unique()

				
					<?php

$array1 = ['apple', 'banana', 'cherry'];
$array2 = ['banana', 'date', 'apple'];

$mergedArray = array_merge($array1, $array2);
$uniqueArray = array_unique($mergedArray);

print_r($uniqueArray);

?>

				
			

Output :

				
					Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [4] => date
)

				
			

In this example, array_merge() combines the two arrays, and array_unique() removes duplicate values, resulting in an array with unique values.

Method 2 : Using array_merge() with array_flip()

Another method to ensure unique values is by using array_flip() after merging. This technique works well when dealing with strings or integers.

Example 2 : Merging Arrays with array_merge() and array_flip()

				
					<?php

$array1 = ['apple', 'banana', 'cherry'];
$array2 = ['banana', 'date', 'apple'];

$mergedArray = array_merge($array1, $array2);
$uniqueArray = array_keys(array_flip($mergedArray));

print_r($uniqueArray);

?>

				
			

Output :

				
					Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => date
)

				
			

Here, array_merge() combines the arrays, array_flip() swaps the values with keys, and array_keys() returns the unique values as keys.

Method 3 : Using array_diff() and array_merge()

This method involves using array_diff() to find unique values before merging.

Example 3 : Merging Arrays with array_diff() and array_merge()

				
					<?php

$array1 = ['apple', 'banana', 'cherry'];
$array2 = ['banana', 'date', 'apple'];

$uniqueArray2 = array_diff($array2, $array1);
$uniqueArray = array_merge($array1, $uniqueArray2);

print_r($uniqueArray);

?>

				
			

Output :

				
					Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => date
)

				
			

In this example, array_diff() finds elements in $array2 that are not in $array1, and array_merge() combines the unique values.

Method 4 : Using Loops for Custom Merging

For more control, you can use loops to merge arrays and ensure uniqueness.

Example 4 : Merging Arrays with a Loop

				
					<?php

$array1 = ['apple', 'banana', 'cherry'];
$array2 = ['banana', 'date', 'apple'];

$uniqueArray = $array1;

foreach ($array2 as $value) {
    if (!in_array($value, $uniqueArray)) {
        $uniqueArray[] = $value;
    }
}

print_r($uniqueArray);

?>

				
			

Output :

				
					Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => date
)

				
			

This example uses a foreach loop to add elements from $array2 to $uniqueArray only if they are not already present.

Conclusion :

Merging two arrays with unique values in PHP can be achieved through several methods. Whether you use array_merge() combined with array_unique(), array_flip(), array_diff(), or custom loops, each method offers a way to ensure your merged array contains only unique values. Choose the method that best fits your needs and coding style.

We hope this article has provided you with the knowledge to merge arrays effectively in PHP. Happy coding!

Scroll to Top