- array_reverse: documentation ( source)
- array_chunk: documentation ( source)
- range: documentation ( source)
- str_pad: documentation ( source)
<?php
$max = 100; // The last number
$cols = 5; // The point at which a new line will start
$arr = array_chunk(range(1, $max), $cols); // Magic ;D
// Print the data.
foreach ($arr as $key => $row) {
// This will reverse every other row
$row = ($key % 2 === 0) ? $row : array_reverse($row);
foreach ($row as $value) {
$value = str_pad($value, strlen($max), ' ', STR_PAD_LEFT);
echo "{$value} ";
}
echo "\n";
}