What is the method of merging and removing duplicates from arrays in PHP?

In PHP, you can use the array_merge() function to combine arrays. This function takes two or more arrays as parameters and returns a merged array.

However, the array_merge() function will keep duplicate values without removing them from the merged array. If there is a need to remove duplicate values from the merged array, the array_unique() function can be used. This function takes an array as a parameter and returns an array containing only unique values.

Here is an example code demonstrating how to merge arrays and remove duplicates using the functions array_merge() and array_unique().

$array1 = [1, 2, 3];
$array2 = [3, 4, 5];

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

print_r($uniqueArray);

The output result is:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [4] => 4
    [5] => 5
)

Please note that the array_unique() function will keep the first occurrence of a value and remove any subsequent duplicates.

bannerAds