note 119185 added to function.array-chunk

From: Date: Sun, 17 Apr 2016 10:55:13 +0000
Subject: note 119185 added to function.array-chunk
Groups: php.notes 
Request: Send a blank email to php-notes+get-205250@lists.php.net to get a copy of this message
A breakdown by groups with excess: function array_chunk_greedy($arr, $count){ $arr = array_chunk($arr, $count); if(($k = count($arr)-1) > 0){ if(count($arr[$k]) < $count){ $arr[$k-1] = array_merge($arr[$k-1], $arr[$k]); unset($arr[$k]); } } return $arr; } $arr = range(1, 13); $arr = array_chunk_greedy($arr, 4); print_r($arr); [1,2,3,4,5,6,7,8,9,10,11,12,13] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13] More examples: [1,2,3,4,5,6,7,8,9,10,11,12] —> [1,2,3,4] [5,6,7,8] [9,10,11,12] [1,2,3,4,5,6,7,8,9,10,11,12,13] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13] [1,2,3,4,5,6,7,8,9,10,11,12,13,14] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13,14] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] —> [1,2,3,4] [5,6,7,8] [9,10,11,12,13,14,15] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] —> [1,2,3,4] [5,6,7,8] [9,10,11,12] [13,14,15,16] Example report: $arr = range(1, 45); $arr = array_chunk_lazy($arr, 10); $arr = array_map(function($sub_value) { return implode('<br>', $sub_value); }, $arr); $title = '<h2>title</h2>'; $arr = $title.implode($title, $arr).$title; echo $arr; ---- Server IP: 72.52.91.14 Probable Submitter: 95.106.104.178 ---- Manual Page -- http://php.net/manual/en/function.array-chunk.php Edit -- https://master.php.net/note/edit/119185 Del: integrated -- https://master.php.net/note/delete/119185/integrated Del: useless -- https://master.php.net/note/delete/119185/useless Del: bad code -- https://master.php.net/note/delete/119185/bad+code Del: spam -- https://master.php.net/note/delete/119185/spam Del: non-english -- https://master.php.net/note/delete/119185/non-english Del: in docs -- https://master.php.net/note/delete/119185/in+docs Del: other reasons-- https://master.php.net/note/delete/119185 Reject -- https://master.php.net/note/reject/119185 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205250) next »