How to Merge Two Arrays with Unique Values in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Remove String Keys in PHP Array?
- How to Remove Numeric Keys in PHP Array?
- How to Remove String Values in PHP Array?
- How to Remove Numeric Values in PHP Array?
- How to Get First 10 Elements of Array in PHP?
- How to Get First 5 Elements of Array in PHP?
- How to Get First 3 Elements of Array in PHP?
- How to Get First 2 Elements of Array in PHP?
- How to Get Last 10 Elements of Array in PHP?
- How to Get Last 5 Elements of Array in PHP?
- How to Get Random Value from an Array in PHP?
- How to Get Last Element of Array in PHP?
- How to Get Minimum Key Value of Array in PHP?
- How to Remove Null Values from Array in PHP?