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

藏色散人
Release: 2023-03-09 08:34:01
Original
1546 people have browsed it

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...)
Copy after login

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:

"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge_recursive($a1,$a2));
?>
Copy after login

Output:

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

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!

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 [email protected]
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!