PHP 8.5.0 Alpha 2 available for testing

Voting

: six minus zero?
(Example: nine)

The Note You're Voting On

Anonymous
19 years ago
Here my array_chunk_values( ) with values distributed by lines (columns are balanced as much as possible) :

<?php
function array_chunk_vertical($data, $columns) {
$n = count($data) ;
$per_column = floor($n / $columns) ;
$rest = $n % $columns ;

// The map
$per_columns = array( ) ;
for (
$i = 0 ; $i < $columns ; $i++ ) {
$per_columns[$i] = $per_column + ($i < $rest ? 1 : 0) ;
}

$tabular = array( ) ;
foreach (
$per_columns as $rows ) {
for (
$i = 0 ; $i < $rows ; $i++ ) {
$tabular[$i][ ] = array_shift($data) ;
}
}

return
$tabular ;
}

header('Content-Type: text/plain') ;

$data = array_chunk_vertical(range(1, 31), 7) ;
foreach (
$data as $row ) {
foreach (
$row as $value ) {
printf('[%2s]', $value) ;
}
echo
"\r\n" ;
}

/*
Output :

[ 1][ 6][11][16][20][24][28]
[ 2][ 7][12][17][21][25][29]
[ 3][ 8][13][18][22][26][30]
[ 4][ 9][14][19][23][27][31]
[ 5][10][15]
*/
?>

<< Back to user notes page

To Top