How to Merge Two Array in PHP?
Hello Developer,
This article goes in detailed on how to merge two array in php. I explained simply about how to merge two array in php laravel. you will learn how to append two arrays in php. you will learn how to merge multiple array in php. you will do the following things for php merge two array example.
we will use array_merge() function and + sign to merge multiple array in php. i will give you simple two examples. so, let's see the simple code of how to merge array in php.
Example 1:
index.php
<?php
$arrayOne = ["One", "Two", "Three"];
$arrayTwo = ["Four", "Five"];
$newArray = 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) "Four"
[4]=> string(4) "Five"
}
Example 2:
index.php
<?php
$arrayOne = [1 => "One", 2 => "Two", 3 => "Three"];
$arrayTwo = [4 => "Four", 5 => "Five"];
$newArray = $arrayOne + $arrayTwo;
var_dump($newArray);
?>
Output:
array(5) {
[1]=> string(3) "One"
[2]=> string(3) "Two"
[3]=> string(5) "Three"
[4]=> string(4) "Four"
[5]=> string(4) "Five"
}
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 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 First Element of Array in PHP?
- PHP Multidimensional Array Search By Value Example
- How to Get Maximum Key Value of Array in PHP?
- How to Remove Empty Values from Array in PHP?
- How to Get Minimum Key Value of Array in PHP?
- How to Remove Null Values from Array in PHP?