How to Merge Two Arrays in PHP?

Posted by

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.

Example 1:

<?php
  
    $arrayOne = ["One", "Two", "Three"];
    $arrayTwo = ["Four", "Five"];
        
    $newArray = array_merge($arrayOne, $arrayTwo);
    
    var_dump($newArray);
  
?>

Output: 1

Example: 2

<?php
  
    $arrayOne = [1 => "One", 2 => "Two", 3 => "Three"];
    $arrayTwo = [4 => "Four", 5 => "Five"];
        
    $newArray = $arrayOne + $arrayTwo;
    
    var_dump($newArray);
  
?>

Output: 2

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x