Merge Arrays with array_merge()
Write a PHP function that takes two arrays and returns a single merged array using array_merge(). array_merge() appends the elements of the second array to the first, re-indexing numeric keys.
array_merge() is a PHP function that merges one or more arrays. For numeric keys, the values are appended and re-indexed. For string keys, later values overwrite earlier ones. It creates a new array without modifying the originals.
This is the standard way to combine arrays in PHP, unlike the + operator which preserves the first array's keys and discards duplicates from the second.
Time complexity is O(n + m) where n and m are the array lengths. The function creates a new array by copying elements from each input.
Example Input & Output
Merge two simple arrays
Different sizes
Empty first array
Empty second array
All empty
Algorithm Flow

Solution Approach
Best Answers
<?php
function solution($arr1, $arr2) {
return array_merge($arr1, $arr2);
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
