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

[PHP Learning] Multiple methods to merge arrays

little bottle
little bottle forward
2019-04-23 15:57:09 2693browse

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 "
"; var_dump($result1); var_dump($result2);

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. Mergethrough

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 "
"; var_dump($result1); var_dump($result2);

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 '
'; var_dump($result);

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 asarray_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, whilearray_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 "
"; var_dump($result2);

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete