PHP array_merge() Function
Palavras-chave:
Publicado em: 06/08/2025Understanding the PHP array_merge() Function
The array_merge()
function in PHP is a powerful tool for combining multiple arrays into a single array. This article provides a comprehensive guide to understanding and utilizing the array_merge()
function, including its usage, underlying principles, and alternative approaches.
Fundamental Concepts / Prerequisites
To effectively understand the array_merge()
function, a basic understanding of PHP arrays is essential. You should be familiar with array indexing (both numeric and associative), array creation, and basic array manipulation. Knowledge of PHP functions and their arguments is also assumed.
Core Implementation/Solution
Here's a code example demonstrating how to use the array_merge()
function:
<?php
// Example 1: Merging two indexed arrays
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$mergedArray1 = array_merge($array1, $array2);
print_r($mergedArray1); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
// Example 2: Merging two associative arrays
$array3 = ['a' => 'apple', 'b' => 'banana'];
$array4 = ['c' => 'cherry', 'd' => 'date'];
$mergedArray2 = array_merge($array3, $array4);
print_r($mergedArray2); // Output: Array ( [a] => apple [b] => banana [c] => cherry [d] => date )
// Example 3: Merging with string keys that conflict
$array5 = ['a' => 'apple', 'b' => 'banana'];
$array6 = ['a' => 'avocado', 'c' => 'cherry'];
$mergedArray3 = array_merge($array5, $array6);
print_r($mergedArray3); // Output: Array ( [a] => avocado [b] => banana [c] => cherry ) 'a' is overwritten
// Example 4: Merging with integer keys that conflict
$array7 = [0 => 'apple', 1 => 'banana'];
$array8 = [0 => 'avocado', 2 => 'cherry'];
$mergedArray4 = array_merge($array7, $array8);
print_r($mergedArray4); // Output: Array ( [0] => avocado [1] => banana [2] => cherry ) keys are reindexed.
// Example 5: Merging multiple arrays
$array9 = [1, 2];
$array10 = [3, 4];
$array11 = [5, 6];
$mergedArray5 = array_merge($array9, $array10, $array11);
print_r($mergedArray5); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
?>
Code Explanation
The code demonstrates several scenarios using array_merge()
. In the first example, two numerically indexed arrays are merged. The resulting array contains all elements from both arrays. In the second example, two associative arrays are merged. The resulting array contains all key-value pairs from both arrays.
Example 3 illustrates how array_merge()
handles duplicate string keys. In this case, the later array's value overwrites the earlier array's value for the same key. Example 4 demonstrates how numerically indexed keys are *re-indexed* if there are integer key conflicts. This behavior is crucial to understanding when dealing with numerical keys. Lastly, Example 5 shows that array_merge()
can handle multiple arrays simultaneously.
Complexity Analysis
The time complexity of array_merge()
is typically O(n), where n is the total number of elements in all the arrays being merged. This is because each element from the input arrays needs to be copied into the resulting array. In the worst-case scenario (e.g., merging many large arrays), the performance could be affected.
The space complexity is also O(n), as the function needs to create a new array large enough to hold all the elements from the input arrays. Memory allocation depends directly on the sum of the sizes of the input arrays.
Alternative Approaches
An alternative to array_merge()
is to use the array union operator (`+`). However, the union operator behaves differently when dealing with duplicate keys. For numeric keys, it preserves the first array's values. For string keys, if a key exists in both arrays, the element from the left-hand array will be used, and the element from the right-hand array will be ignored. This behavior can be desirable in some situations, but it is important to be aware of the differences between array_merge()
and the union operator to choose the appropriate approach for a specific task.
<?php
$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['a' => 'avocado', 'c' => 'cherry'];
$unionArray = $array1 + $array2;
print_r($unionArray); // Output: Array ( [a] => apple [b] => banana [c] => cherry )
?>
Conclusion
The array_merge()
function is a useful function in PHP to combine multiple arrays into one. It's essential to remember the behavior of array_merge()
, particularly how it handles numeric and string keys, and how it deals with duplicate string keys. Consider alternatives like the `+` operator and their impact on your code's logic. Understanding these nuances enables developers to write more efficient and predictable PHP code.