How to Merge Two Array in PHP?

By Hardik Savani May 14, 2024 Category : 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...

Tags :
Shares