The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk.
Syntax:
array array_chunk( $array, $size, $preserve_keys )
Parameters: This function accepts three parameters as shown in the above syntax. The parameters are described below:
- $array: This parameter represents the array that is needed to be divided into chunks.
- $size: This parameter is an integer which defines the size of the chunks to be created.
- $preserve_keys: This parameter takes Boolean value. When this parameter is set to TRUE then the keys are preserved, otherwise the chunk is reindexed starting from 0.
Return value: This function returns a multidimensional array indexed starting from 0. Each chunk contains $size number of elements, except the last chunk which may contain lesser number of elements.
Examples:
Input : $input_array = array('a', 'b', 'c', 'd', 'e');
array_chunk($input_array, 2);
Output : Array(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Input : $input_array = array('a', 'b', 'c', 'd', 'e');
array_chunk($input_array, 2, true)
Output : Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
)
In the first example a multidimensional array is returned in which each chunk contains 2 elements. In the second example, since the third argument is passed as true therefore the index of elements in each chunk is the same as their index in the original array from which the chunk is created. In this case, each chunk contains 2 elements which are the value of size passed to the function.
Below programs illustrate the array_chunk() function in PHP:
Program 1:
<?php $input_array = array('a', 'b', 'c', 'd', 'e'); print_r(array_chunk($input_array, 2)); ?> |
Output:
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Program 2:
<?php $input_array = array('a', 'b', 'c', 'd', 'e'); print_r(array_chunk($input_array, 2, true)); ?> |
Output:
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
)
Reference: http://php.net/manual/en/function.array-chunk.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | Ds\Set first() Function
- PHP | Ds\Set last() Function
- PHP | pow( ) Function
- PHP | next() Function
- p5.js | nfs() Function
- D3.js | d3.set.add() Function
- PHP | each() Function
- p5.js | box() Function
- p5.js | nfc() function
- p5.js | nf() Function
- D3.js | d3.map.set() Function
- PHP | Ds\Set add() Function
- p5.js | nfp() Function
- D3.js | d3.hsl() Function
- PHP | end() Function
- PHP | max( ) Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

