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

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

PHP中文网
Release: 2023-03-16 19:40:01
Original
1778 people have browsed it

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));
?>
Copy after login

Definition and usage

array_chunk() Function splits an array into new array chunks.

Syntax

array_chunk(array,size,preserve_keys);
Copy after login
<?php 
/*
参数      描述
array      必需。规定要使用的数组。    
size       必需。一个整数,规定每个新数组块包含多少个元素。    
preserve_key    可选。可能的值:
true -        保留原始数组中的键名。
false -       默认。每个新数组块使用从零开始的索引。
*/
?>
Copy after login

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));
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template