Home  >  Article  >  Backend Development  >  An in-depth chat about the array_chunk() function in php

An in-depth chat about the array_chunk() function in php

PHP中文网
PHP中文网Original
2017-10-27 09:14:311685browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn