(PHP 4 >= 4.2.0, PHP 5)
array_chunk — 将一个数组分割成多个
$input
, int $size
[, bool $preserve_keys
] )
array_chunk() 将一个数组分割成多个数组,其中每个数组的单元数目由
size
决定。最后一个数组的单元数目可能会少几个。得到的数组是一个多维数组中的单元,其索引从零开始。
将可选参数 preserve_keys
设为 TRUE
,可以使 PHP
保留输入数组中原来的键名。如果你指定了 FALSE
,那每个结果数组将用从零开始的新数字索引。默认值是 FALSE
。
Example #1 array_chunk() 例子
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
以上例程会输出:
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 ) )
input
The array to work on
size
The size of each chunk
preserve_keys
When set to TRUE
keys will be preserved.
Default is FALSE
which will reindex the chunk numerically
Returns a multidimensional numerically indexed array, starting with zero,
with each dimension containing size
elements.
If size
is less than 1
E_WARNING
will be thrown and NULL
returned.
Example #2 array_chunk() example
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
以上例程会输出:
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 ) )