How can the script be modified to ensure that each team has unique players?

To ensure that each team has unique players, we can modify the script to keep track of which players have already been assigned to a team. We can create an array to store the players assigned to each team and check this array before assigning a player to a team.

$teams = ['Team A', 'Team B', 'Team C'];
$players = ['Player 1', 'Player 2', 'Player 3', 'Player 4', 'Player 5'];

$team_players = array_fill_keys($teams, []);

foreach ($teams as $team) {
    $available_players = array_values(array_diff($players, $team_players[$team]));
    $random_player = $available_players[array_rand($available_players)];
    
    $team_players[$team][] = $random_player;
    
    echo "$team: $random_player\n";
}