PHP 中的 array_chunk() 函数
phpprogrammingserver side programming更新于 2025/9/20 13:57:00
array_chunk() 函数将数组拆分为数组块。它返回一个多维数字索引数组,从零开始。
语法
array_chunk(arr, chunk_size, retain_key)
参数
arr − 数组
chunk_size − 块的大小(整数)
preserve_key − 它具有以下值:TRUE- 保留键,FALSE:重新索引块。
返回
array_chunk() 函数返回一个多维数字索引数组,从零开始。
以下是显示如何拆分数组的示例。
示例
<?php
$products = array("Electronics"=>"99","Accessories"=>"110","Clothing"=>"150","Furniture"=>"198");
print_r(array_chunk($products,2,true));
?>
输出
Array
(
[0] => Array
(
[Electronics] => 99
[Accessories] => 110
)
[1] => Array
(
[Clothing] => 150
[Furniture] => 198
)
)

