Home >Backend Development >PHP Problem >Why don't php two-dimensional arrays overlap and merge?

Why don't php two-dimensional arrays overlap and merge?

藏色散人
藏色散人Original
2021-03-31 09:17:261752browse

php Method to merge two-dimensional arrays without recursive: First create a PHP sample file; then create two two-dimensional arrays; finally merge the arrays through the "array_merge_recursive" function.

Why don't php two-dimensional arrays overlap and merge?

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

The array_merge_recursive() function in PHP can Implement

Merge the elements of one or more arrays, and the values ​​in one array are appended to the previous array. and returns the resulting array.

When there are duplicate key names, the value will not be overwritten, but multiple values ​​with the same key name will be recursively formed into an array.

array_merge_recursive(array1,array2,array3...)

Parameter description

array1 Required. The first array of input.

array2 Required. The second array of input.

array3 Optional. Multiple input arrays can be specified.

For example:

<?php
$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge_recursive($a1,$a2));
?>

Output:

Array (
[a] => Horse
[b] => Array ( [0] => Dog [1] => Cat )
[c] => Cow
)

Note: The array_merge() function can also be implemented. Unlike the array_merge_recursive() function, if the key name is repeated, the key The key value is the value corresponding to the last key name (the latter one overwrites the previous one). If the array is numerically indexed, the key names are re-indexed consecutively.

[Recommended learning: PHP video tutorial]

The above is the detailed content of Why don't php two-dimensional arrays overlap and merge?. 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