Post Details

How to Merge Two Array in PHP
18 Nov

How to Merge Two Array in PHP

Hello Developer,

This article goes into detail on how to merge two arrays in PHP. I explained simply how to merge two arrays in PHP Laravel. you will learn how to append two arrays in PHP. you will learn how to merge multiple arrays in PHP. you will do the following things for php merge two array example.

we will use the array_merge() function and + sign to merge multiple arrays in PHP. I will give you simple two examples. so, let's see the simple code of how to merge an 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...

0 Comments

Leave a Comment