note 63394 added to function.array-chunk

From: Date: Wed, 22 Mar 2006 00:19:16 +0000
Subject: note 63394 added to function.array-chunk
Groups: php.notes 
Request: Send a blank email to php-notes+get-106187@lists.php.net to get a copy of this message
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] */ ?> ---- Server IP: 217.174.203.10 Probable Submitter: 82.228.123.113 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.array-chunk.php Edit -- http://master.php.net/note/edit/63394 Del: integrated -- http://master.php.net/note/delete/63394/integrated Del: useless -- http://master.php.net/note/delete/63394/useless Del: bad code -- http://master.php.net/note/delete/63394/bad+code Del: spam -- http://master.php.net/note/delete/63394/spam Del: non-english -- http://master.php.net/note/delete/63394/non-english Del: in docs -- http://master.php.net/note/delete/63394/in+docs Del: other reasons-- http://master.php.net/note/delete/63394 Reject -- http://master.php.net/note/reject/63394 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106187) next »