Transferred from: http://cn2.php.net/manual/zh/function.array-chunk.php
(PHP 4 >= 4.2.0, PHP 5)
array_chunk- splits an array into multiple
Descriptionarray array_chunk ( array
$input
, int
$size
[, bool
$preserve_keys
= false])
Splits an array into multiple arrays, each of which is determined by the number of cells in each array size
. The number of cells in the last array may be less than size
one.
Parameters
-
input
-
Array to operate on
-
size
-
Number of cells per array
-
preserve_keys
-
Set to TRUE
allow PHP to keep the original key name in the input array. If you specify FALSE
, then each result array will be indexed with a new zero-based number. The default value is FALSE
.
return value
The resulting array is a cell in a multidimensional array whose index starts at zero and each dimension contains size
elements.
Error/exception
If size
it is less than 1, an error is thrown E_WARNING
and returned NULL
.
Example
Example #1 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));
?>
The above routines will output:
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 ))