PHP array_chunk() 函数

PHP 教程 · 2019-01-21 23:14:46

实例

把数组分割为带有两个元素的数组块:

<?php
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
?>

定义和用法

array_chunk() 函数把一个数组分割为新的数组块。

语法

array_chunk(array,size,preserve_keys);

参数 描述
array 必需。规定要使用的数组。
size 必需。一个整数,规定每个新数组块包含多少个元素。
preserve_key 可选。可能的值:
  • true - 保留原始数组中的键名。
  • false - 默认。每个新数组块使用从零开始的索引。

技术细节

返回值: 返回一个多维的数值数组,从 0 开始,每个维度都包含 size 元素。
PHP 版本: 4.2+

更多实例

实例 1

把数组分割为带有两个元素的数组块,并保留原始数组中的键名:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
print_r(array_chunk($age,2,true));
?>

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

The Book of CSS3

The Book of CSS3

Peter Gasston / No Starch Press / 2011-5-13 / USD 34.95

CSS3 is the technology behind most of the eye-catching visuals on the Web today, but the official documentation can be dry and hard to follow. Luckily, The Book of CSS3 distills the heady technical la......一起来看看 《The Book of CSS3》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具