3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ ['id' => 1, 'nickname' => 'abc', 'game' => 'abc', 'checkbox' => 1], ['id' => 2, 'nickname' => 'xyz', 'game' => 'zyx', 'checkbox' => 0], ['id' => 5, 'nickname' => 'xyz', 'game' => 'abc', 'checkbox' => 1], ['id' => 6, 'nickname' => 'afg', 'game' => 'zyx', 'checkbox' => 1], ]; // The data must be sorted by the game, that's the key $sortedData = [ ['id' => 1, 'nickname' => 'abc', 'game' => 'abc', 'checkbox' => 1], ['id' => 5, 'nickname' => 'xyz', 'game' => 'abc', 'checkbox' => 1], ['id' => 2, 'nickname' => 'xyz', 'game' => 'zyx', 'checkbox' => 0], ['id' => 6, 'nickname' => 'afg', 'game' => 'zyx', 'checkbox' => 1], ]; // Figure out which game should span how many rows ahead of time foreach ($sortedData as $row) { $gameCounts[$row['game']] ??= 0; $gameCounts[$row['game']]++; } echo '<table border="1">'; echo '<thead>'; echo '<tr>'; foreach (array_keys($sortedData[0]) as $header) { echo '<th>'.$header.'</th>'; } echo '</tr>'; echo '</thead>'; echo '<tbody>'; $previousGame = null; foreach ($sortedData as $row) { echo '<tr>'; foreach ($row as $key => $value) { if ($key === 'game') { // Game changed - span the cell over the correct number of rows if ($previousGame !== $value) { echo '<td rowspan="'.$gameCounts[$value].'">'.$value.'</td>'; $previousGame = $value; } continue; } echo '<td>'.$value.'</td>'; } echo '</tr>'; } echo '</tbody>'; echo '</table>';

preferences:
29.22 ms | 404 KiB | 5 Q