3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Gridizer { /** * Get a squar-ish grid */ public function getGrid($array) { $grid_size = floor(sqrt(count($array))); $table = $this->chunk($table_cells, max($grid_size, 1)); foreach ($table as $row) { echo implode(' | ', $row) . PHP_EOL; } } /** * Create a Generator to chunk an incoming array into `$chunk_size` sized chunks */ public function chunk(array $array, $chunk_size) { $index = 0; while ($index < count($array)) { yield array_slice($array, $index, $chunk_size, true); $index += $chunk_size; } } } $foo = new Gridizer; echo $foo->getGrid(range(1, 100));

preferences:
51.43 ms | 402 KiB | 5 Q