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>';
Output for 8.1.29 - 8.1.33, 8.2.20 - 8.2.29, 8.3.5 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
<table border="1"><thead><tr><th>id</th><th>nickname</th><th>game</th><th>checkbox</th></tr></thead><tbody><tr><td>1</td><td>abc</td><td rowspan="2">abc</td><td>1</td></tr><tr><td>5</td><td>xyz</td><td>1</td></tr><tr><td>2</td><td>xyz</td><td rowspan="2">zyx</td><td>0</td></tr><tr><td>6</td><td>afg</td><td>1</td></tr></tbody></table>
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
96.75 ms | 407 KiB | 5 Q