3v4l.org

run code in 300+ PHP versions simultaneously
<?php $teams = array('All-Stars', 'Vets', 'Lightning', 'Bolt'); // HOW MANY WEEKS $weeks = 3; // MAKE ENOUGH ARRAY ELEMENTS FOR THE DISTRIBUTION $array = array_merge($teams, $teams); // POPULATE THE MATCHES ARRAY $matches = array(); while ($weeks) { foreach ($teams as $ptr => $team) { // FIND THE INDEX INTO THE DISTRIBUTION ARRAY $linkt = $ptr + $weeks; // SELECT THE HOME AND AWAY TEAMS $home = $team; $away = $array[$linkt]; $matches[$team][$weeks] = array('home' => $home, 'away' => $away); } // NEXT WEEK $weeks--; } // SORT THE MATCHES SENSIBLY SO WEEK ONE COMES FIRST foreach ($matches as $team => $contests) { ksort($contests); $matches[$team] = $contests; } // ACTIVATE THIS TO SEE WHAT THE $matches ARRAY LOOKS LIKE // print_r($matches); // CREATE THE TABLE OF MATCHUPS $out = NULL; $out .= "<table>"; $out .= PHP_EOL; // CREATE THE HEADERS FOR EACH WEEK $weeknums = end($matches); $out .= "<tr>"; $out .= '<th> Team </th>'; $out .= '<th> v </th>'; $out .= "<th> Team </th>"; $out .= '</tr>'; $out .= PHP_EOL; // CREATE THE MATRIX OF MATCHUPS foreach ($matches as $team => $contests) { $out .= "<form class='form-horizontal' action='".$_SERVER['PHP_SELF']."'d method='post'><tr><td><input type='text' name='teamone' value='$team' readonly></td>"; $out .= "<td> <b>v</b></td>"; foreach ($contests as $week => $matchup) { // print_r($matchup); $out .= "<td> <input type='text' name='teamtwo' value='{$matchup["away"]}' readonly> </td>"; } $out .= "</tr>"; $out .= PHP_EOL; } $out .= "<input class='btn btn-primary' type='submit' name='submit'></form></table>"; $out .= PHP_EOL; foreach ($matches as $team => $contests) { foreach ($contests as $week => $matchup) { print_r($matchup); if(is_array($matchup)){ foreach($matchup as $key => $value){ $home = $matchup['home']; $away = $matchup['away']; echo "INSERT INTO tourn_fixtures(teamone, teamtwo) values ('$home', '$away')\n"; } } } } echo "</pre>"; //echo $out;

preferences:
16.19 ms | 402 KiB | 5 Q