Home >Backend Development >PHP Problem >How to combine two-dimensional numbers in php without changing the key value

How to combine two-dimensional numbers in php without changing the key value

青灯夜游
青灯夜游Original
2021-09-30 15:09:252824browse

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.

How to combine two-dimensional numbers in php without changing the key value

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:

How to combine two-dimensional numbers in php without changing the key value

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!

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