PHP array_chunk()Functions

PHP Array 参考手册Complete PHP Array Reference Manual

Examples

Split an array into chunks with two elements:

<?php
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
?>

Run Example »

Definition and Usage

The array_chunk() function splits an array into new array chunks.


Syntax

array_chunk(array,size,preserve_keys);

Parameters Description
array Required. Specifies the array to use.
size Required. An integer specifying how many elements each new array chunk contains.
preserve_key Optional. Possible values:
  • true - Preserve the keys of the original array.
  • false - Default. Each new array chunk uses a zero-based index.

Technical Details

Return Value: Returns a multidimensional numeric array, starting from 0, each dimension containingsizeelements.
PHP Version: 4.2+


More Examples

Example 1

Split the array into chunks with two elements, and preserve the keys of the original array:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
print_r(array_chunk($age,2,true));
?>

Run Example »


PHP Array 参考手册Complete PHP Array Reference Manual Other Extensions