How can PHP loops be utilized to format and send array data in emails efficiently?

When sending array data in emails, PHP loops can be utilized to efficiently format the data before sending it. By looping through the array elements, you can concatenate them into a structured format that is suitable for email content. This approach allows for dynamic handling of array data without the need for manual formatting each element.

// Sample array data
$data = array(
    'Name' => 'John Doe',
    'Email' => 'johndoe@example.com',
    'Phone' => '555-555-5555'
);

// Initialize email content
$email_content = '';

// Loop through array data to format email content
foreach ($data as $key => $value) {
    $email_content .= $key . ': ' . $value . "\n";
}

// Send email with formatted array data
$to = 'recipient@example.com';
$subject = 'Array Data Email';
$headers = 'From: sender@example.com';
mail($to, $subject, $email_content, $headers);