PHP array_chunk

Last Updated: February 9, 2022

array_chunk() PHP function can chunk array into chunks and small chunks will have a specified number of elements.

Syntax

array_chunk(array, size, preserve_key) 

Example

In the following example, each chunk will have two elements

<?php
$days=array("Sun","Mon","Tue","Wed","Thu","Fri");
print_r(array_chunk($days,2));
?> 

// Output
Array ( 
    [0] => Array ( [0] => Sun [1] => Mon ) 
    [1] => Array ( [0] => Tue [1] => Wed ) 
    [2] => Array ( [0] => Thu [1] => Fri )
) 

Parameters of array_chunk(array,preserve_key)

Parameter Description
array Required. Specify the array to be used
size Required. Number of elements of the chunk
preserve_key Optional: Possible values:
true – Preserves the keys
false – Default. Reindexes the chunk numerically

Other Details

Test