Skip to content
PHP Logo

Are there any specific PHP functions or techniques that can help in displaying data in columns instead of rows in a table?

When displaying data in columns instead of rows in a table, you can achieve this by using a loop to iterate over your data and organizing it into columns. One way to accomplish this is by using the array_chunk() function in PHP to split your data into chunks representing each column, and then iterating over these chunks to create the columns in your table.

php
$data = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$num_columns = 3;
$columns = array_chunk($data, ceil(count($data) / $num_columns));

echo '<table>';
for ($i = 0; $i < count($columns[0]); $i++) {
    echo '<tr>';
    foreach ($columns as $column) {
        echo '<td>' . $column[$i] . '</td>';
    }
    echo '</tr>';
}
echo '</table>';