PHP array_chunk() Function

What is PHP array_chunk() Function?

If you want to split an array into smaller chunks where each chunk contains a certain number of array elements, use array_chunk() function. Note that the last chunk may contain less number of elements if there doesn’t remains equal number of elements for it.

Syntax:

array_chunk(array, length, preserve_keys)

Parameters:

The Function has 2 required parameters and 1 optional parameter-

array (Required): it is the array that the function splits.

length (Required): it is a number that specifies size of each chunk (meaning number of array elements in each chunk). Check example 1.

preserve_keys (Optional): it is a Boolean value that specify whether or not the keys of the main array are preserved in the smaller chunks. So, it might have two values-

  • TRUE – if the parameter value is TRUE, the keys are preserved. Check example 2.
  • FALSE – if the parameter value is FALSE, the keys are not preserved and re-indexed numerically from starting with 0. Check example 3.

Return Values:

The function returns the array to a numerically indexed multidimensional array. Each inner array contains element with same length except the last one whose length may or may not equal.

Examples:

Example 1:

<pre>
<?php
$languages = array(
    "D" => "Dart",
    "E" => "Erlang",
    "G" => "Go",
    "J" => "Java",
    "M" => "MATLAB",
    "P" => "PHP",
    "R" => "Rust"
);
print_r(array_chunk($languages, 2));
?>
</pre>

Output:

Array
(
    [0] => Array
        (
            [0] => Dart
            [1] => Erlang
        )
    [1] => Array
        (
            [0] => Go
            [1] => Java
        )
    [2] => Array
        (
            [0] => MATLAB
            [1] => PHP
        )
    [3] => Array
        (
            [0] => Rust
        )
)

Explanation:

The function splits the array into a multidimensional array with 2 elements each. Note the last chunk has just one element.

Example 2:

<pre>
<?php // #2
$languages = array(
    "D" => "Dart",
    "E" => "Erlang",
    "G" => "Go",
    "J" => "Java",
    "M" => "MATLAB",
    "P" => "PHP",
    "R" => "Rust"
);
print_r(array_chunk($languages, 2, TRUE));
?>
</pre>

Output:

Array(
    [0] => Array
        (
            [D] => Dart
            [E] => Erlang
        )
     [1] => Array
        (
            [G] => Go
            [J] => Java
        )
     [2] => Array
        (
            [M] => MATLAB
            [P] => PHP
        )
     [3] => Array
        (
            [R] => Rust
        )
 )

Explanation:

The function splits the array into a multidimensional array with two elements each. Note the last chunk has just one element. As the third parameter is TRUE, the keys are preserved inside the chunks.

Example 3:

<pre>
<?php // #3
$languages = array(
    "D" => "Dart",
    "E" => "Erlang",
    "G" => "Go",
    "J" => "Java",
    "M" => "MATLAB",
    "P" => "PHP",
    "R" => "Rust"
);
print_r(array_chunk($languages, 2, FALSE));
?>
</pre>

Output:

Array
(
    [0] => Array
        (
            [0] => Dart
            [1] => Erlang
        )
    [1] => Array
        (
            [0] => Go
            [1] => Java
        )
    [2] => Array
        (
            [0] => MATLAB
            [1] => PHP
        )
    [3] => Array
        (
            [0] => Rust
        )
)

Explanation:

The function splits the array into a multidimensional array with two elements each. Note the last chunk has just one element. As the third parameter is FALSE, the keys are not preserved inside the chunks and start with 0.

Practical Usages of () Function:

It has many usefulness. It helps you by splitting a large array into a smaller chunks so that you can manage it effectively. You can apply it in pagination, displaying a large array data into table rows and columns.

Errors/Exceptions:

If the second parameter (length) is less than 1 ex. 0. -1 etc, the function throws a ValueError. Check example-

<?php
$languages = array(
    "D" => "Dart",
    "E" => "Erlang",
    "G" => "Go",
    "J" => "Java",
    "M" => "MATLAB",
    "P" => "PHP",
    "R" => "Rust"
);
print_r(array_chunk($languages, 0));
?>

Output:

Fatal error:  Uncaught ValueError: array_chunk(): Argument #2 ($length) must be greater than 0 in D:\xampp\htdocs\php\array_chunk.php:11
Stack trace:
#0 D:\xampp\htdocs\phpExercise\515. array_chunk function.php(58): array_chunk(Array, 0)
#1 {main}  thrown in D:\xampp\htdocs\php\array_chunk.php on line 11

PHP Version Support:

PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8

Summary: PHP () Function

When you want to split a large array data into smaller chunks, use array_chunk() function. It is a built-in PHP array function.

Reference:

https://www.php.net/manual/en/function.array-chunk.php