Home > Article > Backend Development > How to merge arrays in php without overwriting duplicate values
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.
Recommended: "PHP Video Tutorial"
Method to merge arrays and retain key values:
<?php $form_data1 = array(11=>'A',12=>'B',13=>'C',14=>'D'); $form_data2 = array(25=>'B',26=>'A',27=>'D',28=>'C'); $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!