PHP array_chunk () function
Examples
The array is divided into blocks with an array of two elements:
<?php
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
?>
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
?>
Running instance »
Definition and Usage
array_chunk () function to split an array into a new array blocks.
grammar
array_chunk( array , size , preserve_keys );
参数 | 描述 |
---|---|
array | 必需。规定要使用的数组。 |
size | 必需。一个整数,规定每个新数组块包含多少个元素。 |
preserve_key | 可选。可能的值:
|
technical details
return value: | It returns a multidimensional array of values, starting at 0, each dimension containing size elements. |
---|---|
PHP version: | 4.2+ |
More examples
Example 1
The array is divided into blocks with an array of two elements, and retain the original array keys:
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
print_r(array_chunk($age,2,true));
?>
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
print_r(array_chunk($age,2,true));
?>
Running instance »
Complete PHP Array Reference