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

How to merge arrays in php without overwriting duplicate values

藏色散人
Release: 2023-03-04 09:20:02
Original
2651 people have browsed it

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

Output:

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

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!

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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template