What are the ways to merge two arrays in php

爱喝马黛茶的安东尼
Release: 2023-02-23 12:32:01
Original
7481 people have browsed it

What are the ways to merge two arrays in php

##1. array_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 in the figure below:

What are the ways to merge two arrays in php

When ordinary arrays are combined, the second array will be placed behind the first array and returned after splicing.

Related recommendations: "

PHP Introduction Tutorial"

But for an array of key-value pairs, if there are the same keys, then the second array will overwrite the first An array of values ​​corresponding to the same key.

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:

What are the ways to merge two arrays in php

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

3. Connect two arrays

array_combine() 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:

What are the ways to merge two arrays in php

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() function is the same as array_merge(). It can merge two or more arrays together to form a joint 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 to form a new array with the original keys. as an 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:

What are the ways to merge two arrays in php

The above is the detailed content of What are the ways to merge two arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!