array_chunk() function in php
Complete PHP Array Reference Manual
Split the array into array chunks with two elements:
<?php $cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel"); print_r(array_chunk($cars,2)); ?>
Definition and usage
array_chunk() Function splits an array into new array chunks.
Syntax
array_chunk(array,size,preserve_keys);
<?php /* 参数 描述 array 必需。规定要使用的数组。 size 必需。一个整数,规定每个新数组块包含多少个元素。 preserve_key 可选。可能的值: true - 保留原始数组中的键名。 false - 默认。每个新数组块使用从零开始的索引。 */ ?>
Return value:
Returns a multi-dimensional numerical array, starting from 0, and each dimension contains size elements. PHP version: 4.2+
Split the array into array blocks with two elements, and retain the key names in the original array:
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50"); print_r(array_chunk($age,2,true)); ?>
The above is the detailed content of An in-depth chat about the array_chunk() function in php. For more information, please follow other related articles on the PHP Chinese website!