How can arrays be effectively used to replace placeholders in HTML templates with dynamic data in PHP?

Arrays can be effectively used to replace placeholders in HTML templates with dynamic data in PHP by storing the dynamic data in key-value pairs within an array, and then using a loop to iterate over the array and replace the placeholders in the HTML template with the corresponding values.

<?php
// Dynamic data stored in an array
$data = array(
    'name' => 'John Doe',
    'email' => 'johndoe@example.com',
    'phone' => '123-456-7890'
);

// HTML template with placeholders
$template = '<p>Name: {name}</p><p>Email: {email}</p><p>Phone: {phone}</p>';

// Replace placeholders with dynamic data
foreach ($data as $key => $value) {
    $template = str_replace("{" . $key . "}", $value, $template);
}

// Output the updated HTML template
echo $template;
?>