How can PHP developers handle the creation of multiple dynamic maps using Google Maps API in a single script effectively?

When creating multiple dynamic maps using Google Maps API in a single script, PHP developers can effectively handle this by using a loop to iterate through the data for each map and dynamically generate the necessary JavaScript code for each map within the loop. This approach allows for the creation of multiple maps with unique markers, styles, and settings in a concise and organized manner.

<?php
// Data for multiple maps
$mapsData = array(
    array(
        'mapId' => 'map1',
        'lat' => 37.7749,
        'lng' => -122.4194,
        'zoom' => 12,
        'markers' => array(
            array('lat' => 37.7749, 'lng' => -122.4194, 'title' => 'San Francisco')
        )
    ),
    array(
        'mapId' => 'map2',
        'lat' => 34.0522,
        'lng' => -118.2437,
        'zoom' => 10,
        'markers' => array(
            array('lat' => 34.0522, 'lng' => -118.2437, 'title' => 'Los Angeles')
        )
    )
);

// Loop through maps data and generate JavaScript code for each map
foreach ($mapsData as $mapData) {
    echo "<div id='{$mapData['mapId']}' style='height: 400px;'></div>";
    echo "<script>
            var map{$mapData['mapId']} = new google.maps.Map(document.getElementById('{$mapData['mapId']}'), {
                center: {lat: {$mapData['lat']}, lng: {$mapData['lng']},
                zoom: {$mapData['zoom']}
            });

            var marker{$mapData['mapId']} = new google.maps.Marker({
                position: {lat: {$mapData['markers'][0]['lat']}, lng: {$mapData['markers'][0]['lng']},
                map: map{$mapData['mapId']},
                title: '{$mapData['markers'][0]['title']}'
            });
          </script>";
}
?>