Latest web development tutorials

PHP array_chunk () function

PHP Array Reference Complete PHP Array Reference

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));
?>

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 可选。可能的值:
  • true - 保留原始数组中的键名。
  • false - 默认。每个新数组块使用从零开始的索引。

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));
?>

Running instance »


PHP Array Reference Complete PHP Array Reference