Home > Article > Backend Development > How to merge multiple arrays into one array and merge the same keys in two-dimensional arrays
The array looks like this
Now I want to create the following effect
Masters, please help me
The array looks like this
Now I want to create the following effect
Masters, please help me
Append array
The array_merge_recursive() function is the same as array_merge(). It 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 to form a new array with the original keys. as an array name. There is also a form of array merging, which is to recursively append arrays. Its form is:
array_merge_recursive(array array1,array array2[…,array arrayN])
Program examples are as follows:
`$fruit1 = array("apple" => "red", "banana" => "yellow");
$fruit2 = array("pear" => "yellow", "apple" => "green");
$result = array_merge_recursive($fruit1, $fruit2);
print_r($result);
// output
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow ) `
array_merge
<code>array_merge_recursive</code>