PHP Program to Merge two Sorted Arrays
Given two sorted arrays, i.e. arr1, and arr2, the task is to merge both sorted arrays and make a single sorted array.
Examples:
Input: arr1 = [1, 3, 5, 7, 8] arr2 = [2, 3, 4, 5, 6] Output: [1, 2, 3, 3, 4, 5, 6, 7, 8]
Below are the methods to merge both arrays:
Table of Content
Using Loop
Here, we iterate through both arrays and compare array elements to merge them into a new sorted array.
Example:
<?php
function mergeArrays($arr1, $arr2) {
$mergedArray = [];
$i = $j = 0;
while ($i < count($arr1) && $j < count($arr2)) {
if ($arr1[$i] < $arr2[$j]) {
$mergedArray[] = $arr1[$i];
$i++;
} else {
$mergedArray[] = $arr2[$j];
$j++;
}
}
while ($i < count($arr1)) {
$mergedArray[] = $arr1[$i];
$i++;
}
while ($j < count($arr2)) {
$mergedArray[] = $arr2[$j];
$j++;
}
return $mergedArray;
}
$arr1 = [1, 3, 5, 7];
$arr2 = [2, 4, 6, 8];
$newArray = mergeArrays($arr1, $arr2);
echo "Merged Sorted Array: " . implode(", ", $newArray);
?>
Output
Merged Sorted Array: 1, 2, 3, 4, 5, 6, 7, 8
Using array_merge() Function
The array_merge() function is used to merge two or more arrays into a single array. After merging the both arrays, we use sort() function to sort the merged array in ascending order.
Example 1: In this example, we will merge two sorted array to make a single sorted array.
<?php
function mergeArrays($arr1, $arr2) {
$mergedArray = array_merge($arr1, $arr2);
sort($mergedArray);
return $mergedArray;
}
$arr1 = [1, 3, 5, 7];
$arr2 = [2, 4, 6, 8];
$newArray = mergeArrays($arr1, $arr2);
echo "Merged Sorted Array: " . implode(", ", $newArray);
?>
Output
Merged Sorted Array: 1, 2, 3, 4, 5, 6, 7, 8
Example 2: In this example, merge two sorted array.
<?php
function mergeSortedArrays($arr1, $arr2) {
// Merge the arrays
$mergedArray = array_merge($arr1, $arr2);
// Sort the merged array
sort($mergedArray);
return $mergedArray;
}
// Example usage
$arr1 = [1, 3, 5, 7];
$arr2 = [2, 4, 6, 8];
$mergedArray = mergeSortedArrays($arr1, $arr2);
print_r($mergedArray);
?>
Output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 )
Using array_unique() Function
This program merges two sorted arrays using array_merge() function to combine them and array_unique() function to remove duplicates, ensuring the merged array remains sorted with unique elements.
<?php
function mergeSortedArrays(array $arr1, array $arr2) {
// Merge the arrays
$mergedArray = array_merge($arr1, $arr2);
// Remove duplicates and sort
$mergedArray = array_unique($mergedArray);
sort($mergedArray);
return $mergedArray;
}
// Example usage
$arr1 = [1, 3, 5, 7];
$arr2 = [2, 4, 6, 8];
$mergedArray = mergeSortedArrays($arr1, $arr2);
print_r($mergedArray);
?>
Output
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 )
Using array_merge() and array_unique() Functions
This approach combines array_merge() function to merge the arrays and array_unique() function to remove duplicates while keeping the array sorted.
<?php
$arr1 = [1, 3, 5, 7, 8];
$arr2 = [2, 3, 4, 5, 6];
// Merge arrays and remove duplicates
// while maintaining order
$mergedArray = array_unique(array_merge($arr1, $arr2));
print_r($mergedArray);
?>
Output
Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 8 [5] => 2 [7] => 4 [9] => 6 )