PHP array_chunk() Function

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

array_chunk(array, length, preserve_keys)

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.

  • TRUE: Specify when you want to preserve to keys.
  • FALSE: You can specify to reindex the chunks numerically that start from 0. It is the default for the parameter.

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.

Output

Array ( [0] => Array ( [0] => Cycle [1] => Bike ) [1] => Array ( [0] => Car [1] => Bolero ) )

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.

Output

Array ( [0] => Array ( [0] => Cycle [1] => Bike [2] => Car ) [1] => Array ( [0] => Bolero [1] => Racer ) )

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.

Output

Array ( [0] => Array ( [0] => Cycle [1] => Bike ) [1] => Array ( [2] => Car [3] => Bolero ) )

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

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.