How can PHP be used to create a route planner between multiple locations with known travel times?

To create a route planner between multiple locations with known travel times using PHP, we can utilize algorithms such as Dijkstra's or A* to find the shortest path between the locations. We can represent the locations as nodes and the travel times as edges in a graph data structure. By implementing the algorithm in PHP, we can calculate the optimal route based on the given travel times.

// Define the graph representing the locations and travel times
$graph = [
    'A' => ['B' => 10, 'C' => 15],
    'B' => ['A' => 10, 'C' => 5],
    'C' => ['A' => 15, 'B' => 5]
];

// Function to find the shortest path using Dijkstra's algorithm
function dijkstra($graph, $start, $end) {
    $distances = [];
    $previous = [];
    $queue = new SplPriorityQueue();

    foreach ($graph as $node => $edges) {
        $distances[$node] = INF;
        $previous[$node] = null;
        foreach ($edges as $neighbor => $distance) {
            $queue->insert($neighbor, $distance);
        }
    }

    $distances[$start] = 0;

    while (!$queue->isEmpty()) {
        $current = $queue->extract();
        foreach ($graph[$current] as $neighbor => $distance) {
            $alt = $distances[$current] + $distance;
            if ($alt < $distances[$neighbor]) {
                $distances[$neighbor] = $alt;
                $previous[$neighbor] = $current;
            }
        }
    }

    $path = [];
    $current = $end;
    while ($current !== null) {
        array_unshift($path, $current);
        $current = $previous[$current];
    }

    return $path;
}

// Find the shortest path between locations 'A' and 'C'
$route = dijkstra($graph, 'A', 'C');
print_r($route);