Are there any best practices for handling multidimensional arrays in PHP when merging data from different sources?

When merging data from different sources into multidimensional arrays in PHP, it is important to ensure that the keys do not clash and that the data is merged correctly. One approach is to use the array_merge_recursive function to merge arrays while preserving the keys and values.

// Example of merging multidimensional arrays from different sources
$data1 = [
    'key1' => 'value1',
    'key2' => [
        'subkey1' => 'subvalue1'
    ]
];

$data2 = [
    'key2' => [
        'subkey2' => 'subvalue2'
    ],
    'key3' => 'value3'
];

$mergedData = array_merge_recursive($data1, $data2);

print_r($mergedData);