Home > Backend Development > PHP Tutorial > [PHP Learning] Multiple methods to merge arrays

[PHP Learning] Multiple methods to merge arrays

little bottle
Release: 2023-04-06 09:48:01
forward
2829 people have browsed it

The main content of this article is the various methods to implement array merging in PHP, which has certain reference value. Interested friends can learn about it.

1.arrary_merge

Sample code:

$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(1, 2, 6, 7, 8, 9, 10);
$result1 = array_merge($arr1, $arr2);

$arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male");
$arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male");
$result2 = array_merge($arr3, $arr4);

echo "<pre class="brush:php;toolbar:false">";
var_dump($result1);
var_dump($result2);
Copy after login

Run the above code, the output result is as shown below:

[PHP Learning] Multiple methods to merge arrays

When merging ordinary arrays, the second array will be placed behind the first array and returned after splicing.
But for an array of key-value pairs, if there are the same keys, the second array will overwrite the values ​​corresponding to the same keys in the first array.

2. Merge through

Sample code:

$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(1, 2, 6, 7, 8, 9, 10);
$result1 = $arr1 + $arr2;

$arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male");
$arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male", "id" => "411521");
$result2 = $arr3 + $arr4;

echo "<pre class="brush:php;toolbar:false">";
var_dump($result1);
var_dump($result2);
Copy after login

Run the above code, the output result is as shown below:

[PHP Learning] Multiple methods to merge arrays

It can be seen that when using " " to merge two arrays, whether it is an ordinary array or a key-value array, as long as the subscript is the same or the key is the same, the former will overwrite the latter. . This needs attention.

3. Connect two arrays

array_combine()The function will get a new array, which consists of a set of submitted keys and corresponding values.
Sample code:

$arr1 = array("A","B","C","D"); 
$arr2 = array("paul","itbsl","Golang","PHP"); 
$result = array_combine($arr1,$arr2); 
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
var_dump($result);
Copy after login

Run the above code, the output result is as shown below:

[PHP Learning] Multiple methods to merge arrays
Note: If you need to merge the arrays into In the above form, the lengths of the two merged arrays must be equal, that is, count($arr1) == count($arr2), and cannot be empty

4. Recursively append arrays

array_merge_recursive()The function is the same as array_merge(), which can merge two or more arrays together to form a combined array. The difference between the two is that the function will handle it differently when a key in an input array already exists in the result array. array_merge() will overwrite the previously existing key/value pairs and replace them with the key/value pairs in the current input array, while array_merge_recursive() will merge the two values ​​together, Form a new array and use the original keys as the array name.
Sample code:

$arr3 = array("name" => "itbsl", "age" => 13, "sex" => "Male");
$arr4 = array("name" => "火龙果", "age" => 13, "sex" => "Male");
$result2 = array_merge_recursive($arr3, $arr4);

echo "<pre class="brush:php;toolbar:false">";
var_dump($result2);
Copy after login

Run the above code, the output result is as shown below:

[PHP Learning] Multiple methods to merge arrays

Related tutorials:PHP video tutorial

The above is the detailed content of [PHP Learning] Multiple methods to merge arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template