How to Merge Two Arrays with Unique Values in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hello Artisan,

Today our leading topic is how to merge two array with unique values in php. This tutorial will give you a simple example of how to merge array with unique values in php. This tutorial will give you a simple example of how to merge arrays without duplicates in php. I explained simply about php array merge array unique. Let's get started with php array merge without duplicates values.

we will use array_merge() function and array_unique() to merge array with unique values in php. so, let's see the simple code of how to merge two arrays without duplicates values in php.

Example:

index.php

<?php

$arrayOne = ["One", "Two", "Three", "Five"];

$arrayTwo = ["Two", "Three", "Four", "Five"];

$newArray = array_unique(array_merge($arrayOne, $arrayTwo));

var_dump($newArray);

?>

Output:

array(5) {

[0]=> string(3) "One"

[1]=> string(3) "Two"

[2]=> string(5) "Three"

[3]=> string(4) "Five"

[6]=> string(4) "Four"

}

I hope it can help you...

Tags :
Shares