How can data structures in PHP be utilized to manage and redirect URLs based on specific IDs for widget applications?

To manage and redirect URLs based on specific IDs for widget applications in PHP, we can utilize associative arrays to map the IDs to their corresponding URLs. We can then use the $_GET superglobal to retrieve the ID from the URL and redirect the user to the appropriate URL based on the ID.

<?php
// Define an associative array mapping IDs to URLs
$widgetUrls = [
    1 => 'https://www.example.com/widget1',
    2 => 'https://www.example.com/widget2',
    3 => 'https://www.example.com/widget3',
];

// Retrieve the ID from the URL
$id = isset($_GET['id']) ? $_GET['id'] : null;

// Redirect the user to the appropriate URL based on the ID
if (array_key_exists($id, $widgetUrls)) {
    header('Location: ' . $widgetUrls[$id]);
    exit();
} else {
    echo 'Invalid widget ID';
}
?>