Last Updated on July 28, 2021 by Roshan Parihar
PHP array_chunk() Function is used to break the array into chunks or parts according to the specified size. However, the last part or chunk may not be of the given size you want to create.
Syntax of PHP array_chunk() Function
The function takes three arguments to pass and create chunks. The first argument is the array you want to use to break into parts. The second argument is to specify the length of the chunk. The third argument is to define whether you want to preserve the keys or not.
Description of Parameters
Name | Description |
---|---|
array | Required. It is the array you have to specify to break into parts. |
length | Required. Specify the number of chunks or parts you want to create from an array. |
preserve_keys | optional. It is the boolean value to specify.
|
Let’s see some examples that are useful to understand the use of the PHP array_chunk()
Function in PHP.
Examples of PHP array_chunk() Function
See the example given below to learn the use of the function and break the array into chunks.
Example 1: Break Array into Chunks of Two
If you want to convert the array into chunks of two, you have to use the PHP array_chunk()
Function and pass 2 as the second argument. It breaks the array into small parts in which each part contains 2 elements that are chunks of two.
1 2 3 4 5 6 7 8 9 10 |
<?php //Declare an array in PHP $myarray = array("Cycle", "Bike", "Car", "Bolero"); //Break into chunks of 2 $myArrChunks = array_chunk($myarray, 2); //Print result print_r($myArrChunks); ?> |
Output
The above example prints the array in the output. The first part of an array contains two elements, the second part contains two elements.
Example 2: Split Array into Parts of Three
You can split the array into parts of three using the function. It requires passing 3 as the second argument of the function to get the required chunks of the array in PHP.
1 2 3 4 5 6 7 8 9 10 |
<?php //Declare an array in PHP $myarray = array("Cycle", "Bike", "Car", "Bolero", "Racer"); //Split array into parts of 3 $myArrChunks = array_chunk($myarray, 3); //Print result print_r($myArrChunks); ?> |
Output
The above example contains five elements. After the function split the array into parts of three, you will get two arrays in which the first contains three elements and the second contains two items.
Example 3: Preserve Keys and Break Array Into Chunks
In addition to the above methods, you can also preserve the keys and break the array into chunks using the PHP array_chunk()
Function. You need to pass the boolean value ‘TRUE’ as the third argument of the function to preserve the keys.
1 2 3 4 5 6 7 8 9 10 |
<?php //Declare an array in PHP $myarray = array("Cycle", "Bike", "Car", "Bolero"); //Preserve keys and break array into chunks $myArrChunks = array_chunk($myarray, 2, true); //Print result print_r($myArrChunks); ?> |
Output
The output given in the above example comes out after it preseves the keys of an array in PHP.
You May Also Like to Read