What are some best practices for handling duplicate keys in a multidimensional array in PHP?

When dealing with duplicate keys in a multidimensional array in PHP, one common approach is to check if the key already exists before adding a new element. If a duplicate key is found, you can choose to overwrite the existing value, ignore the new value, or handle it in a custom way based on your requirements.

// Example of handling duplicate keys in a multidimensional array

$array = array(
    'key1' => array('value1', 'value2'),
    'key2' => array('value3', 'value4'),
    'key1' => array('value5', 'value6') // Duplicate key
);

// Checking for duplicate keys and overwriting the existing value
$newKey = 'key1';
if (array_key_exists($newKey, $array)) {
    $array[$newKey] = array('new_value1', 'new_value2');
} else {
    $array[$newKey] = array('value5', 'value6');
}

print_r($array);