array_chunk()
Last updated onWhen working with big arrays in PHP, things can get messy. That is where
steps in by providing a way to get those big arrays broken down into small, manageable pieces, from organizing data that is to be displayed to splitting content into rows or handling large datasets cleanly and efficiently using php array_chunk()
. array_chunk()
In this article, you will learn what the PHP function
does and how to use it, along with practical examples, and why it's a convenient tool in the hands of any PHP developer.array_chunk()
What is php array_chunk()?
In other words, the
in PHP is a function that takes one array and breaks it into smaller arrays or, in other words, chunks: take a whole long list of items and divide it into portions that are easier to handle or manage. array_chunk()
Be it displaying products across rows, creating groups, or processing large sets of data,
will do what is required.array_chunk()
Syntax of PHP array_chunk()
Accordingly, the syntax is relatively simple; there are only a couple of main options:
array_chunk(array $array, int $size, bool $preserve_keys = false): array
: This is the source array that you would want to split up.$array
: Size that the chunks should contain.$size
(optional): Whether to preserve the original keys of the array. If$preserve_keys
TRUE
, PHP will keep the keys intact.
The next section explains how
works with a simple example.array_chunk()
Basic Example of php array_chunk()
Suppose you have an array of students and you want to divide them into groups of three. Here is how
does just that for you in PHP:array_chunk()
$students = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Frank"];
$groups = array_chunk($students, 3);
print_r($groups);
Here is the output:
Array
(
[0] => Array ( [0] => Alice [1] => Bob [2] => Charlie )
[1] => Array ( [0] => Dave [1] => Eve [2] => Frank )
)
Above,
divided our array_chunk()
array into two smaller arrays of three each. It's a clean and clear-cut way to handle grouping without any excess loops or conditions.$students
Now, in the next section, we will see how the
parameter works and when you might need it.preserve_keys
Using preserve_keys with the array_chunk()
By default,
will reset the keys to begin at 0 for each chunk. But what if you needed to retain the original keys, say you are dealing with associative arrays? Well, that's where array_chunk()
comes in. Here it is in action:preserve_keys
$student_scores = ["Alice" => 95, "Bob" => 87, "Charlie" => 92, "Dave" => 88];
$groups = array_chunk($student_scores, 2, true);
print_r($groups);
Here is the output:
Array
(
[0] => Array ( [Alice] => 95 [Bob] => 87 )
[1] => Array ( [Charlie] => 92 [Dave] => 88 )
)
This will work, but setting
to preserve_keys
, the original names are kept as keys for readability and ease of use. If true
was not set, this function would reset the keys in each chunk.preserve_keys
Wrapping Up
This
function in PHP is powerful yet simple in terms of splitting an array into parts. Ideally, it's great for creating manageable chunks in items being displayed in rows, handling large datasets, and batch grouping of data. array_chunk()
Once you get accustomed to the syntax, optional parameters, and use, you'll commit it smoothly to your PHP projects. Just keep in mind how chunk size and key preservation affect the handling of your data, and you will squeeze the most from
.array_chunk()
Frequently Asked Questions (FAQs)
What is `array_chunk()`?
How does `array_chunk()` work?
How do you preserve keys in `array_chunk()`?
What is the syntax for `array_chunk()`?
What happens if the array length isn’t a perfect multiple of the chunk size?
Can `array_chunk()` handle empty arrays?
Is `array_chunk()` available in all PHP versions?
When should I use `array_chunk()` over other array functions?
Did you find this tutorial useful?
Your feedback helps us improve our tutorials.