What potential pitfalls should be considered when using a multidimensional array in conjunction with the mail() function in PHP?

When using a multidimensional array in conjunction with the mail() function in PHP, a potential pitfall to consider is that the mail() function expects a flat array for the headers parameter. If you pass a multidimensional array, it may cause unexpected behavior or errors. To solve this issue, you can flatten the multidimensional array into a single-dimensional array before passing it to the mail() function.

// Flatten a multidimensional array into a single-dimensional array
function flatten_array($array) {
    $result = [];
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $result = array_merge($result, flatten_array($value));
        } else {
            $result[$key] = $value;
        }
    }
    return $result;
}

// Example usage
$multidimensional_array = [
    'To' => ['recipient@example.com'],
    'Subject' => 'Test Email',
    'Headers' => [
        'From' => 'sender@example.com',
        'Reply-To' => 'reply@example.com'
    ]
];

$flattened_array = flatten_array($multidimensional_array);

// Send email using mail() function with flattened array
mail($flattened_array['To'], $flattened_array['Subject'], '', $flattened_array['Headers']);