note 119428 added to function.array-chunk
| From: | suisuiruyi at aliyun dot com | Date: | Fri, 03 Jun 2016 09:02:18 +0000 |
| Subject: | note 119428 added to function.array-chunk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205649@lists.php.net to get a copy of this message | ||
chunk array vertically
$arr = range(1, 19);
function array_chunk_vertical($arr, $percolnum){
$n = count($arr);
$mod = $n % $percolnum;
$cols = floor($n / $percolnum);
$mod ? $cols++ : null ;
$re = array();
for($col = 0; $col < $cols; $col++){
for($row = 0; $row < $percolnum; $row++){
if($arr){
$re[$row][] = array_shift($arr);
}
}
}
return $re;
}
$result = array_chunk_vertical($arr, 6);
foreach($result as $row){
foreach($row as $val){
echo '['.$val.']';
}
echo '<br/>';
}
/*
[1][7][13][19]
[2][8][14]
[3][9][15]
[4][10][16]
[5][11][17]
[6][12][18]
*/
----
Server IP: 72.52.91.14
Probable Submitter: 117.158.156.184
----
Manual Page -- http://php.net/manual/en/function.array-chunk.php
Edit -- https://master.php.net/note/edit/119428
Del: integrated -- https://master.php.net/note/delete/119428/integrated
Del: useless -- https://master.php.net/note/delete/119428/useless
Del: bad code -- https://master.php.net/note/delete/119428/bad+code
Del: spam -- https://master.php.net/note/delete/119428/spam
Del: non-english -- https://master.php.net/note/delete/119428/non-english
Del: in docs -- https://master.php.net/note/delete/119428/in+docs
Del: other reasons-- https://master.php.net/note/delete/119428
Reject -- https://master.php.net/note/reject/119428
Search -- https://master.php.net/manage/user-notes.php