array_chunk() - 将一个数组分割成多个 - php 数组函数
array_chunk()
(PHP 4 >= 4.2.0, PHP 5, PHP 7)
将一个数组分割成多个
说明
array_chunk(array $array,int $size[,bool $preserve_keys= false]): array将一个数组分割成多个数组,其中每个数组的单元数目由$size决定。最后一个数组的单元数目可能会少于$size个。
参数
$array需要操作的数组
$size每个数组的单元数目
$preserve_keys设为TRUE
,可以使 PHP 保留输入数组中原来的键名。如果你指定了FALSE
,那每个结果数组将用从零开始的新数字索引。默认值是FALSE
。
返回值
得到的数组是一个多维数组中的单元,其索引从零开始,每一维包含了$size个元素。
错误/异常
如果$size小于 1,会抛出一个E_WARNING
错误并返回NULL
。
范例
Example #1array_chunk()例子
以上例程会输出:
Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => d ) [2] => Array ( [4] => e ) )
参见
array_slice()
从数组中取出一段
Tried to use an example below (#56022) for array_chunk_fixed that would "partition" or divide an array into a desired number of split lists -- a useful procedure for "chunking" up objects or text items into columns, or partitioning any type of data resource. However, there seems to be a flaw with array_chunk_fixed — for instance, try it with a nine item list and with four partitions. It results in 3 entries with 3 items, then a blank array. So, here is the output of my own dabbling on the matter: Array ( [0] => Array ( [0] => Black Canyon City [1] => Chandler [2] => Flagstaff [3] => Gilbert [4] => Glendale [5] => Globe ) [1] => Array ( [0] => Mesa [1] => Miami [2] => Phoenix [3] => Peoria [4] => Prescott [5] => Scottsdale ) [2] => Array ( [0] => Sun City [1] => Surprise [2] => Tempe [3] => Tucson [4] => Wickenburg ) )
Here my array_chunk_values( ) with values distributed by lines (columns are balanced as much as possible) :
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 '; } /* [1][7][13][19] [2][8][14] [3][9][15] [4][10][16] [5][11][17] [6][12][18] */
Response to azspot at gmail dot com function partition. $columns = 3; $citylist = array('Black Canyon City', 'Chandler', 'Flagstaff', 'Gilbert', 'Glendale', 'Globe', 'Mesa', 'Miami', 'Phoenix', 'Peoria', 'Prescott', 'Scottsdale', 'Sun City', 'Surprise', 'Tempe', 'Tucson', 'Wickenburg'); print_r(array_chunk($citylist, ceil(count($citylist) / $columns))); Output: Array ( [0] => Array ( [0] => Black Canyon City [1] => Chandler [2] => Flagstaff [3] => Gilbert [4] => Glendale [5] => Globe ) [1] => Array ( [0] => Mesa [1] => Miami [2] => Phoenix [3] => Peoria [4] => Prescott [5] => Scottsdale ) [2] => Array ( [0] => Sun City [1] => Surprise [2] => Tempe [3] => Tucson [4] => Wickenburg ) )
If you just want to grab one chunk from an array, you should use array_slice().
To reverse an array_chunk, use array_merge, passing the chunks as a variadic:
This function takes each few elements of an array and averages them together. It then places the averages in a new array. It is used to smooth out data. For example lets say you have a years worth of hit data to a site and you want to graph it by the week. Then use a bucket of 7 and graph the functions output. function array_bucket($array, $bucket_size) // bucket filter { if (!is_array($array)) return false; // no empty arrays $buckets=array_chunk($array,$bucket_size); // chop up array into bucket size units foreach ($buckets as $bucket) $new_array[key($buckets])=array_sum($bucket)/count($bucket); return $new_array; // return new smooth array }
array_chunk() is helpful when constructing tables with a known number of columns but an unknown number of values, such as a calendar month. Example: Outputs: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 The other direction is possible too, with the aid of a function included at the bottom of this note. Changing this line: $rows = array_chunk($values, 7); To this: $rows = array_chunk_vertical($values, 7); Produces a vertical calendar with seven columns: 1 6 11 16 21 26 31 2 7 12 17 22 27 3 8 13 18 23 28 4 9 14 19 24 29 5 10 15 20 25 30 You can also specify that $size refers to the number of rows, not columns: $rows = array_chunk_vertical($values, 7, false, false); Producing this: 1 8 15 22 29 2 9 16 23 30 3 10 17 24 31 4 11 18 25 5 12 19 26 6 13 20 27 7 14 21 28 The function:
[Editors note: This function was based on a previous function by gphemsley at nospam users dot sourceforge.net] For those of you that need array_chunk() for PHPbased on the same syntax, useful about making columns : ---- array_chunk : fixed number of sub-items ---- Array( [0] => Array( [0] => 1 [1] => 2 ) [1] => Array( [0] => 3 [1] => 4 ) [2] => Array( [0] => 5 ) ) ---- array_chunk : fixed number of columns ---- Array( [0] => Array( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array( [0] => 4 [1] => 5 ) )Hi, I've made a function to split an array into chunks based on columns wanted. For example: goal (say, for 3 columns):This function can be used to reverse effect of . Output: array(17) { [0]=> string(17) "Black Canyon City" [1]=> string(8) "Chandler" [2]=> string(9) "Flagstaff" [3]=> string(7) "Gilbert" [4]=> string(8) "Glendale" [5]=> string(5) "Globe" [6]=> string(4) "Mesa" [7]=> string(5) "Miami" [8]=> string(7) "Phoenix" [9]=> string(6) "Peoria" [10]=> string(8) "Prescott" [11]=> string(10) "Scottsdale" [12]=> string(8) "Sun City" [13]=> string(8) "Surprise" [14]=> string(5) "Tempe" [15]=> string(6) "Tucson" [16]=> string(10) "Wickenburg" }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]) [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('
', $sub_value); }, $arr); $title = 'title
'; $arr = $title.implode($title, $arr).$title; echo $arr;Had need to chunk an object which implemented ArrayAccess Iterator Countable. array_chunk wouldn't do it. Should work for any list of things
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)