What are some best practices for merging arrays in PHP while considering potential duplicate keys?
When merging arrays in PHP, it's important to consider potential duplicate keys to avoid overwriting existing values. One way to handle this is by using the "+" operator, which merges two arrays and preserves the keys of the first array if there are duplicates. This ensures that no data is lost during the merge process.
$array1 = ['key1' => 'value1', 'key2' => 'value2'];
$array2 = ['key2' => 'new_value2', 'key3' => 'value3'];
$mergedArray = $array1 + $array2;
print_r($mergedArray);