PHP array_chunk() Function
Example
Split the array into an array with two elements:
<?php $cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel"); print_r(array_chunk($cars,2)); ?>
Definition and Usage
The array_chunk() function splits the array into new array blocks.
where the number of units in each array is determined by size Parameter determines. The number of units in the last array may be a few less.
Optional Parameters preserve_key Is a boolean value that specifies whether the elements of the new array have the same keys as the original array (for associative arrays) or new numeric keys starting from 0 (for indexed arrays). The default is to assign new keys.
Syntax
array_chunk(array,size,preserve_key);
Parameter | Description |
---|---|
array | Required. Specifies the array to be used. |
size | Required. An integer value specifying how many elements each new array should contain. |
preserve_key |
Optional. Possible values:
|
Technical Details
Return Value: | Returns a multidimensional indexed array, starting from 0, with each dimension containing size elements. |
PHP Version: | 4.2+ |
More Examples
Example 1
Split the array into an array with two elements, while preserving the original array keys:
<?php $age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31","David"=>"35"); print_r(array_chunk($age,2,true)); ?>