Break PHP array into 3 columns

2020-03-01 17:23发布

I'm trying to break a PHP array into 3 columns (has to be columns, not rows) so it would look something like this:

Item 1     Item 2     Item 3
Item 4     Item 5     Item 6
Item 7     Item 8     Item 9
Item 10................

The best approach I can think of would be to break the main array into 3 arrays, 1 for each column although I can't work out the best approach to do this - more specifically the criteria I could use to generate the 3 arrays.

7条回答
不美不萌又怎样
2楼-- · 2020-03-01 18:03
header('Content-Type: text/plain');

echo '<table>';

$colNum = 3;
$regNum = 10;

for ($i = 1; $i <= $regNum; $i++) {

    if (($i % $colNum) == 1) {

        echo "\n\t".'<tr>';

    }

    echo "\n\t\t".'<td>'. $i .'</td>';

    if (($i % $colNum) == 0 or $regNum <= $i) {

        echo "\n\t".'</tr>';

    }

}

echo "\n".'</table>';
查看更多
登录 后发表回答