Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_chunk()

Split successive elements into groups of a positive size for batch processing.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

array_chunk(array $array, int $length, bool $preserve_keys = false): array

Split successive elements into groups of a positive size for batch processing. The final group may be shorter. By default each group receives consecutive integer keys, while enabling key preservation keeps the source keys. An empty source produces no groups, and a nonpositive length raises a value error.

Signature

array_chunk(array $array, int $length, bool $preserve_keys = false): array

Example output

[{"4":"A","6":"B"},{"9":"C"}]

Examples

<?php
$queue = [4 => 'A', 6 => 'B', 9 => 'C'];
echo json_encode(array_chunk($queue, 2, true));

Result

[{"4":"A","6":"B"},{"9":"C"}]

Sources