In PHP, there are many ways to merge two or more arrays. This article will introduce some common array merging methods and their usage scenarios.
Method 1: Use the " " operator
Use the " " operator to merge two arrays into one array. This operator appends the second array to the first array, and if there are two elements with the same key, the elements in the second array overwrite the elements in the first array. The sample code is as follows:
<code class="php">$array1 = array('a' => 1, 'b' => 2); $array2 = array('b' => 3, 'c' => 4); $result = $array1 + $array2; print_r($result);</code>
The output result is:
<code>Array ( [a] => 1 [b] => 2 [c] => 4 )</code>
In this example, $array1 contains two key-value pairs ('a' => 1 and 'b' => 2), $array2 contains two key-value pairs ('b' => 3 and 'c' => 4). Use the " " operator to combine them into an array, the result is an array containing three key-value pairs. Since there is a key name in $array2 that is the same as the key name in $array1 ('b'), the 'b' key-value pair in the $array1 array is overwritten.
It should be noted that when using the " " operator to merge arrays, the original array will not be modified, but a new merged array will be returned.
Method 2: Use the array_merge() function
The array_merge() function can merge two or more arrays and return a new array. Unlike the " " operator, the array_merge() function does not change elements with the same key name, but places them at consecutive positions in the new array. The sample code is as follows:
<code class="php">$array1 = array('a' => 1, 'b' => 2); $array2 = array('b' => 3, 'c' => 4); $result = array_merge($array1, $array2); print_r($result);</code>
The output result is:
<code>Array ( [a] => 1 [b] => 3 [c] => 4 )</code>
In this example, $array1 contains two key-value pairs ('a' => 1 and 'b' => 2), $array2 contains two key-value pairs ('b' => 3 and 'c' => 4). Use the array_merge() function to merge them into an array, the result is an array containing three key-value pairs. Since there is an element with the same key name ('b') in the array, the array_merge() function merges 'b' in $array2 and 'b' in $array1 into one element.
It should be noted that the array_merge() function does not change the original array when merging arrays.
Method 3: Use the array_replace() function
The array_replace() function can replace the elements of an array with elements of the same key name in another array. If a key exists only in the first array, it will remain unchanged. If a key exists only in the second array, it will be added to the first array. The sample code is as follows:
<code class="php">$array1 = array('a' => 1, 'b' => 2); $array2 = array('b' => 3, 'c' => 4); $result = array_replace($array1, $array2); print_r($result);</code>
The output result is:
<code>Array ( [a] => 1 [b] => 3 [c] => 4 )</code>
In this example, $array1 contains two key-value pairs ('a' => 1 and 'b' => 2), $array2 contains two key-value pairs ('b' => 3 and 'c' => 4). Use the array_replace() function to combine them into a single array. The result is an array containing three key-value pairs. Since there is an element with the same key name ('b') in the array, the array_replace() function will overwrite 'b' in $array1 with 'b' in $array2.
It should be noted that the array_replace() function will modify the original array.
Conclusion
The above are the commonly used array merging methods in PHP: using the " " operator, array_merge() function and array_replace() function. Use these methods to conveniently merge two or more arrays. When using it, you need to pay attention to the characteristics and differences of each method and choose the appropriate method to solve the problem.
The above is the detailed content of How to merge several arrays in php. For more information, please follow other related articles on the PHP Chinese website!