Home > Backend Development > PHP Tutorial > How to merge multiple arrays into one array and merge the same keys in two-dimensional arrays

How to merge multiple arrays into one array and merge the same keys in two-dimensional arrays

WBOY
Release: 2023-03-03 08:14:01
Original
2679 people have browsed it

The array looks like this

How to merge multiple arrays into one array and merge the same keys in two-dimensional arrays
Now I want to create the following effect

How to merge multiple arrays into one array and merge the same keys in two-dimensional arrays

Masters, please help me

Reply content:

The array looks like this

How to merge multiple arrays into one array and merge the same keys in two-dimensional arrays
Now I want to create the following effect

How to merge multiple arrays into one array and merge the same keys in two-dimensional arrays

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>
Copy after login
Related labels:
php
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