How can one efficiently merge a newly created multidimensional array with an existing one in PHP?

When merging a newly created multidimensional array with an existing one in PHP, you can use the array_merge_recursive() function to combine the arrays while preserving the keys and values of both arrays. This function recursively merges the arrays, allowing you to efficiently merge multidimensional arrays without losing any data.

// Existing multidimensional array
$existingArray = [
    'key1' => 'value1',
    'key2' => [
        'subkey1' => 'subvalue1',
        'subkey2' => 'subvalue2'
    ]
];

// Newly created multidimensional array
$newArray = [
    'key2' => [
        'subkey2' => 'updatedSubvalue2',
        'subkey3' => 'subvalue3'
    ],
    'key3' => 'value3'
];

// Merge the arrays
$mergedArray = array_merge_recursive($existingArray, $newArray);

// Output the merged array
print_r($mergedArray);