PHP array_chunk Function | Split Array Into Chunks

PHP array_chunk Function is an inbuilt function in PHP that splits an array into chunks. The input parameters specify the size of each chunk. However, depending on the size of an array, the last chunk may contain fewer elements. This function does not preserve keys by default. However, that can be changed by passing a parameter. In this article, we will be discussing some common usage of the PHP array_chunk Function.

Syntax

array array_chunk($array, $size, $preserveKeys = false) 

Parameters

PHP array_chunk Function takes three parameters in input. Out of the three, two parameters are mandatory while one is optional. The description of each of the parameter is as follows:

  • $array: Mandatory. The first parameter to the PHP array_chunk Function is the array. The function will split the input array into chunks.
  • $size: Mandatory. The second parameter is an integer size of each chunk. The function will take this input as the size of each chunk in the final array.
  • $preserveKeys: Optional. The third and last parameter is a boolean value. If this parameter is TRUE then the function preserves keys as the original array and vice versa. However, this parameter is optional and its default value is FALSE. By default it will reindex the keys numerically.

Return Value

PHP array_chunk Function returns a multi-dimensional array. Each dimension of the array contains a $size number of elements. However, the last chunk may contain fewer elements depending on the size of the original array.

PHP array_chunk Function Diagram Concatly

Examples

Let’s go through a few examples to demonstrate the usage of PHP array_chunk Function.

Example 1

<?php

	$testArray = range(1,10);
	$finalArray = array_chunk($testArray, 3);
	print_r($finalArray);
	/*
	Array
	(
	    [0] => Array
	        (
	            [0] => 1
	            [1] => 2
	            [2] => 3
	        )
	    [1] => Array
	        (
	            [0] => 4
	            [1] => 5
	            [2] => 6
	        )
	    [2] => Array
	        (
	            [0] => 7
	            [1] => 8
	            [2] => 9
	        )
	    [3] => Array
	        (
	            [0] => 10
	        )
	)
	*/
?>

In the first example, we have an array containing 10 elements (1 to 10). We split the array into chunks of size 3. The final array contains 4 chunks. 3 chunks contain 3 elements, while the last chunk contains only 1 element. This demonstrates the basic usage of PHP array_chunk Function.

Example 2

Also, we can observe in the above example that the function does not preserve keys by default. Let’s pass true in the third parameter and observe the difference in the final array.

<?php
	$testArray = range(1,10);
	$finalArray = array_chunk($testArray, 3, true);
	print_r($finalArray);
	/*
	Array
	(
	    [0] => Array
	        (
	            [0] => 1
	            [1] => 2
	            [2] => 3
	        )
	    [1] => Array
	        (
	            [3] => 4
	            [4] => 5
	            [5] => 6
	        )
	    [2] => Array
	        (
	            [6] => 7
	            [7] => 8
	            [8] => 9
	        )
	    [3] => Array
	        (
	            [9] => 10
	        )
	)
	*/
?>

You should observe that the output is almost same. However, notice a difference in the inner arrays. Instead of each chunk starting from 0, this time the chunks preserve keys from the original array.

Example 3

Consider an example with string keys.

<?php
	$testArray = array(
		'John' => 1,
		'Emma' => 21,
		'Carl' => 4,
		'Ross' => 76,
		'Rick' => 2
	);
	$finalArray = array_chunk($testArray, 2);
	print_r($finalArray);
	/*
	Array
	(
	    [0] => Array
	        (
	            [0] => 1
	            [1] => 21
	        )
	    [1] => Array
	        (
	            [0] => 4
	            [1] => 76
	        )
	    [2] => Array
	        (
	            [0] => 2
	        )
	)
	*/
?>

Again we can observe that the function does not preserve keys by default. However, passing true in the third parameter preserves keys as follows:

	$finalArray = array_chunk($testArray, 2, true);
	print_r($finalArray);
	/*
	Array
	(
	    [0] => Array
	        (
	            [John] => 1
	            [Emma] => 21
	        )
	    [1] => Array
	        (
	            [Carl] => 4
	            [Ross] => 76
	        )
	    [2] => Array
	        (
	            [Rick] => 2
	        )
	)
	*/

Conclusion

We discussed the usage of PHP array_chunk Function in this article. It is very useful in creating batches or dividing a large data set into smaller chunks.

You can read about more Array Functions in PHP. Also, you can read about the PHP array_chunk Function on the Official Documentation of PHP.

Spread the Knowledge

Leave a Reply

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