Home  >  Article  >  Backend Development  >  How to merge arrays in php without overwriting duplicate values

How to merge arrays in php without overwriting duplicate values

藏色散人
藏色散人Original
2020-08-11 10:08:462609browse

How to implement php merging arrays without overwriting: first create a PHP sample file; then define two sets of arrays; then merge the arrays through the "$form_data1 $form_data2;" method; and finally output the merged value.

How to merge arrays in php without overwriting duplicate values

Recommended: "PHP Video Tutorial"

Method to merge arrays and retain key values:

<?php
$form_data1 = array(11=>&#39;A&#39;,12=>&#39;B&#39;,13=>&#39;C&#39;,14=>&#39;D&#39;);
$form_data2 = array(25=>&#39;B&#39;,26=>&#39;A&#39;,27=>&#39;D&#39;,28=>&#39;C&#39;);
$result = $form_data1 + $form_data2;
print_r($result);
?>

Output:

Array
(
    [11] => A
    [12] => B
    [13] => C
    [14] => D
    [25] => B
    [26] => A
    [27] => D
    [28] => C
)

Use the " " operator to merge arrays to retain the key values ​​of the array. If the merged array contains the same key value, the later ones will not overwrite the previous ones. The key value (the first one takes precedence).

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