Home >Backend Development >PHP Problem >What are the methods for merging two arrays in PHP?

What are the methods for merging two arrays in PHP?

DDD
DDDOriginal
2023-06-02 11:31:403567browse

php two array merging method: 1. Create a php sample file, define two variables as "$a" and "$b", and use the " " operator to achieve it through the "$a $b" formula The values ​​of the two arrays are merged into one array; 2. Use the "array_merge($a,$b)" syntax to merge the two arrays; 3. Use the "array_merge_recursive($a,$b)" syntax to merge the arrays; 4. Use the "array_combine($a,$b)" syntax to combine two arrays.

What are the methods for merging two arrays in PHP?

The operating environment of this tutorial: windows10 system, PHP8.1.3 version, DELL G3 computer

php method of merging two arrays is :

1. Use the plus sign " " operator:

Directly merge the values ​​​​of the two arrays into one array;

<?php
$a = [1,2,3,&#39;a&#39;=>&#39;a&#39;];
$b = [&#39;a&#39;=>&#39;b&#39;,4,5,&#39;b&#39;=>&#39;c&#39;,6,7,&#39;a&#39;];
$c = $a + $b;
 
var_dump($a);
var_dump($b);
var_dump($c);
?>

Output result:

What are the methods for merging two arrays in PHP?

Use " " to merge arrays. If the keys are the same, the previous array value will overwrite the following array value.

2. Use the array_merge() function

to directly merge two arrays. If the keys of the two arrays are the same, the value of the latter array overwrites the value of the previous array

<?php
$a = [1,&#39;2&#39;=>2,&#39;a&#39;=>&#39;a&#39;,&#39;b&#39;=>&#39;b&#39;];
$b = [1,&#39;2&#39;=>3,&#39;a&#39;=>&#39;c&#39;,&#39;b&#39;=>&#39;d&#39;,&#39;c&#39;=>&#39;e&#39;];
$c = array_merge($a,$b);
 
var_dump($a);
var_dump($b);
var_dump($c);
?>

Output result:

What are the methods for merging two arrays in PHP?

#It can be seen that when the two array keys are the same, the value of the latter array overwrites the value of the previous array. However, for numeric indexes or numeric string indexes, they will be reset in order (the first numeric index element of the first array is filled in sequence starting from 0)

3. Use the array_merge_recursive() function

Merge arrays, when encountering the same key, merge the values ​​in the key into a sub-array

<?php
$a = [1,&#39;2&#39;=>2,&#39;a&#39;=>&#39;a&#39;,&#39;b&#39;=>&#39;b&#39;];
$b = [1,&#39;2&#39;=>3,&#39;a&#39;=>&#39;a&#39;,&#39;b&#39;=>&#39;d&#39;,&#39;c&#39;=>&#39;e&#39;];
$c = array_merge_recursive($a,$b);
 
var_dump($a);
var_dump($b);
var_dump($c);
?>

Output result:

What are the methods for merging two arrays in PHP?

##Use "array_merge_recursive" method, the elements with the same key will be integrated into a new array, keeping the key name unchanged, as a sub-element of the merged array (the array combined by $a and $b). But for numeric indexes or numeric string indexes, they will be reset in order (the first numeric index element of the first array is filled sequentially starting from 0). Even if the corresponding numerical indexes are the same, elements with the same index will not be integrated into a subarray.

4. Use the array_combine() function

array_combine() function creates a new array by merging two arrays, where the elements of one array are key names and the elements of the other array are keys. value.

Note: The number of elements in the key name array and the key value array must be the same!

<?php
$a=array("a","b","c","d");
$b=array("red","green","blue","yellow");
$c = array_combine($a,$b);
 
var_dump($a);
var_dump($b);
var_dump($c);
?>

Output result:

What are the methods for merging two arrays in PHP?

The above is the detailed content of What are the methods for merging two arrays in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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