Open In App

How to Concatenate Two Arrays in PHP ?

Concatenating Arrays in PHP involves combining the elements of two or more arrays into a single array. This operation is useful when you need to merge multiple arrays to create a larger array containing all their elements.

Using array_merge() Function

array_merge( ) in PHP combines two or more arrays into a single array, preserving numeric keys and reindexing if keys are not numeric, effectively merging array elements.



Syntax

$result = array_merge($array1, $array2);

Example: Implementation to concatenate arrays.




<?php 
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$result = array_merge($array1, $array2);
print_r($result);
?>

Output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)





Article Tags :