How can the issue of ensuring alternating home and away games be addressed when generating a game schedule in PHP?

Issue: To ensure alternating home and away games when generating a game schedule in PHP, we can create an algorithm that assigns home and away teams in a way that maintains balance and fairness throughout the schedule.

// Sample PHP code snippet to generate a schedule with alternating home and away games
$teams = ['Team A', 'Team B', 'Team C', 'Team D', 'Team E'];

// Shuffle the teams array to randomize the order
shuffle($teams);

// Generate the schedule by pairing teams for each game
for ($i = 0; $i < count($teams) - 1; $i++) {
    if ($i % 2 == 0) {
        echo $teams[$i] . ' vs ' . $teams[$i + 1] . " (Home vs Away)\n";
    } else {
        echo $teams[$i + 1] . ' vs ' . $teams[$i] . " (Home vs Away)\n";
    }
}