PHP array_chunk() function

PHP array_change_key_case() function
PHP array_column function

The PHP array_chunk() method is a sophisticated array manipulation tool that divides an array into smaller, more manageable pieces. Whether you’re dealing with a huge dataset or just want to arrange your data, this function makes it simple to split an array into different sections, giving you greater flexibility over how you interact with it. Whether you want to boost speed or simply make your code more legible and manageable, this method provides a simple yet effective array manipulation solution.

What is the syntax of the array_chunk function in PHP?

array array_chunk ( array $input , int $size [, bool $preserve_keys = false ] )
ParameterDetails
arrayRequired. Array to split into chunks
sizeRequired (integer). Set the size of each chunk
preserve_keyThis is an Optional parameter.
Preserves the keys when set to true
Reindexes the chunk numerically when set to false
Array_chunk function in PHP

The array_chunk() function returns an multidimensional array, containing the chunks as arrays.

Examples of the array_chunk function

Example 1.

$fruits = array("Apple", "Banana", "Cherry", "date", "Elderberry", "Fig", "Grape", "Honeydew");
$chunks = array_chunk($fruits, 2);
print_r($chunks);

In this example, we’ll make a variety of fruits. The array_chunk() method is then used to divide it into 2 parts. The resultant array will be a multidimensional array with four subarrays, the first of which will have two items, the second of which will have two elements, and so on.

Example 2.

$numbers = array(1, 2, 3, 4, 5, 6, 7, 8);
$chunks = array_chunk($numbers, 3, true);
print_r($chunks);

In this example, we’ll make a number array. Then we divide it into three chunks with the array_chunk() method and set the third option to true to keep the keys. The resultant array will be a multidimensional array with three subarrays, the first of which will have three items, the second of which will have three elements, and so on, with the keys intact.

Example 3.

$employee = array("Name" => "John Doe", "Age" => 30, "Salary" => 5000, "Designation" => "Developer");
$chunks = array_chunk($employee, 2, true);
print_r($chunks);

In this example, we’ll make an associative array containing employee information. The array_chunk() method is then used to divide it into chunks of two, and the third parameter is set to true to preserve the keys. The resultant array will be a multidimensional array with two subarrays; the first will have two items, the second will have two elements, and the keys will be kept.

It’s vital to notice that the array_chunk() method returns a new array containing the updated chunks rather than modifying the old array.

It’s also worth noting that if the total number of elements in the array is greater than the chunk size, the last chunk will have less elements than the others.

For more information see the official reference.

PHP array_change_key_case() function
PHP array_column function
en English
X
Scroll to Top