In PHP, you can use the array_merge_recursive() function to merge two-dimensional arrays without changing the key values; this function will not handle the situation where two or more array elements have the same key name. Instead of key name overwriting, multiple values with the same key name are recursively formed into an array.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the array_merge_recursive() function To merge two-dimensional arrays without changing key values.
array_merge_recursive() function is used to merge one or more arrays into one array.
The difference between this function and the array_merge() function is that it handles the situation where two or more array elements have the same key name. array_merge_recursive() does not perform key name overwriting, but recursively combines multiple values with the same key name into an array.
Example: Merge two-dimensional arrays without changing key values
<?php header("Content-type:text/html;charset=utf-8"); $array1 = array( array("张三",25,"男"), array("李四",21,"男"), array("王五",22,"男"), array("娜娜",22,"女"), "PHP中文网" ); $array2 = array( array("小红",25,"女"), array("李四",21,"女"), array("娜娜",22,"女"), "PHP中文网" ); $array3 = array_merge_recursive($array1,$array2); var_dump($array3); ?>
Output:
Recommended learning: "PHP video tutorial"
The above is the detailed content of How to combine two-dimensional numbers in php without changing the key value. For more information, please follow other related articles on the PHP Chinese website!