In this program, we are going to merge two arrays. Here we are using the built-in function array_merge() to merge the array. For example, if the two arrays are [4,5,8,9] and [9,7,6,3] after merging these arrays the result will be [4,5,8,9,9,7,6,3]
Syntax of function array_merge()
array_merge(array1, array2, array3, ...)
To merge two arrays first we have to initialize the arrays arr1 and arr2 with values. Then we have to use the built-in function array_merge()
with arguments arr1 and arr2 to merge the arrays. After that we can print the elements in the arrays arr1[] as the first array, arr2[] as the second array, and arr3[] as the merged array
Step 1: Initialize the arrays arr1 and arr2 with values
Step 2: Use the built-in function array_merge(arr1,arr2) to merge the arrays
Step 3: Print the elements in the arrays arr1[] as the first array, arr2[] as the second array, and arr3[] as the merged array
<?php
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(6, 7, 8, 9, 2);
$arr3 = array_merge($arr1, $arr2);
echo "First array: \n";
foreach ($arr1 as $x) {
echo "$x ";
}
echo "\nSecond array: \n";
foreach ($arr2 as $x) {
echo "$x ";
}
echo "\nArray after merge: \n";
foreach ($arr3 as $x) {
echo "$x ";
}
?>
First array: 1 2 3 4 5 Second array: 6 7 8 9 2 Array after merge: 1 2 3 4 5 6 7 8 9 2