How to merge arrays in PHP

WBOY
Release: 2016-08-08 09:32:16
Original
1296 people have browsed it

1, +

When the key names of the two arrays are numeric key names or string key names, you can use "+" directly, $c = $a +$b, append after $a ($b is in Key name that does not exist in $a) key name and value.
Features:
No overwriting, just appending non-existing key names and corresponding values ​​
Key names are not re-indexed

Whether it is all numeric key names or mixed, just appending keys and values, If the key names are the same, no appending is performed, that is, the first appearing value is returned as the final result.

2, array array_merge(array arra1[, array array2[,array...]])

array_merge() merges the cells of one or more arrays, and appends the values ​​in one array After the previous array, the array of results to be returned.

If the input array contains the same string key name, the value after the key name will overwrite the previous value.

However, if the array contains numeric key names, the subsequent values ​​will not overwrite the original values, but will be appended to them.

If just an array is given and the array is numerically indexed, the key names are re-indexed in a continuous manner

Features:

numeric indexing, no overwriting, after values ​​are merged, The key name will be re-indexed continuously

String key name, then the value after the key will overwrite the previous value

3. array array_merge_recursiv(array array1[,array....])

array_merge_recursive() merges the cells of one or more arrays, appends the values ​​in one array to the previous array, and returns the resulting array.

If the input arrays have the same string key name, then these values ​​will be merged into an array, and so on recursively. Therefore, if a value itself is an array, this function will put it according to the corresponding entry. It is merged into another array.

However, if the array has the same array key name, the latter value will not overwrite the original value, but will be appended to the back

Features:

The rules are basically the same as array_mege, just processing When the string key name is the same, recursive append is used

The above introduces the method of merging arrays in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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!