The easiest way to concatenate two arrays in PHP?

Simply use, array_merge() to concatenate two arrays in PHP. Let’s say the following are out arrays −

$nameArray1 = array('John','David');
$nameArray2 = array('Mike','Sam');

Now, set both the above arrays in array_merge() to concatenate them.

The syntax is as follows −

array_merge($yourFirstArrayName, $yourSecondArrayName);


The PHP code is as follows 

 Live Demo

<!DOCTYPE html>
$nameArray1 = array('John','David');
$nameArray2 = array('Mike','Sam');
$result = array_merge($nameArray1, $nameArray2);


This will produce the following output

Array ( [0] => John [1] => David [2] => Mike [3] => Sam )

Updated on: 13-Oct-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started