Javatpoint Logo
Javatpoint Logo

PHP array_chunk() function

The array_chunk() function is built-in function in PHP. The array_chunk function splits an array into chunks of new arrays. This function divides an array into different block of new arrays.

Syntax

Parameter Description Is compulsory
array Specifies the array to use compulsory
size Integer that specifies the size of each chunk compulsory
preserve key When set to TRUE keys will be preserved. Default is FALSE which will re-index the chunk numerically optional

Important Note: The last chunk may contain less elements than the desired size of the chunk.

Example 1

Output:

Array  (  [0] => Array ( [0] => DOG [1] => CAT) [1] => Array ( [0] => LION [1] => ELEPHANT ) [2] => Array ( [0] => MONKEY 
[1] => TIGER ) )

Example 2

Output:

Array ( [0]=> Array ( [0]=> a  [1]=> b ) [1]=> Array ( [2]=> c [3]=> d ) [2]=> Array ( [4]=> e ) )

Example 3

Output:

Array ( [0] => Array ( [0] => ajay [1] => sonoo [2] => rahul [3] => sid ) [1] => Array ( [0] => raj ) ) Array ( [0] => Array
 ( [0] => ajay [1] => sonoo  ) [1] => Array ( [2]=>rahul [3] => sid  ) [2] => Array ( [4] => raj ) )

Example 4

Output:

Array ( [0] => Array ( [a] => Adobe [b] => Bosch ) [1] => Array ( [c] => Cummins [d] => Dlf  ) )



Help Others, Please Share

facebook twitter pinterest