Home >Backend Development >PHP Problem >How to merge php arrays without duplicates

How to merge php arrays without duplicates

青灯夜游
青灯夜游Original
2021-11-08 18:42:582207browse

In PHP, you can use the array_merge_recursive() function to merge arrays without duplication; this function will not overwrite the key name when processing two or more array elements with the same key name. Instead, multiple values ​​with the same key name are recursively formed into an array.

How to merge php arrays without duplicates

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use the array_merge_recursive() function Implement merged arrays without deduplication.

array_merge_recursive() function merges one or more arrays into one array.

When two or more array elements have the same key name, this function will not overwrite the key name, but will recursively form multiple values ​​with the same key name into an array.

Syntax:

array_merge_recursive(array1,array2,array3...)

Example:

<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
$arr=array_merge_recursive($a1,$a2);
var_dump($arr);
?>

Output result:

1 (1).png

Recommended learning: "PHP video tutorial"

The above is the detailed content of How to merge php arrays without duplicates. 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