Monday, October 19, 2020

PHP array_chunk() Function



Hello folks! welcome to another section of our tutorial on PHP. In this tutorial guide, we are going to be discussing about array_chunk() Function in PHP.

PHP array_chunk() function takes an array as input and then splits that array into chunks of the given size. The last chunk may contain less number of elements than the passed size based on large range of factors of the total numbers available in the array.

Syntax

Following below is the syntax to use this function -

array array_chunk ( array $input, int $size [, bool $preserve_keys] );


Parameter Details

Sr.NoParameter & Description
1

input (mandatory)

This is the input array which we want to slipt into smaller chunks. This is mandatory parameter.

2

size (mandatory)

The size of each chunk we want to split our passed array in the form of input. This is again mandatory parameter.

3

preserve_keys (optional)

This is an optional and boolean parameter but when it is set to TRUE, all the keys in the array will be preserved. If you do not pass it then its default value is FALSE which will reindex the chunk numerically


Return Value

This returns a multidimensional numerical indexed array, starting from zero, with each dimension containing size element.

PHP Version

This function was first lunched as part of core PHP v 4.2.0.

Errors/Exceptions

If passed size is less than 1, the E_WARNING error will be thrown and NULL returned.

Example1

The following example splits an array into multiple chunks of 2 elements each -

<?php
   $input = array('abc', 'bcd', 'cde', 'def', 'efg');
   print_r(array_chunk($input, 2));
?>

Output

When the above code is executed, it will produce the following result. Try to observe the index of each smaller array, it is starting with zero for all the three chunks -

Array
(
    [0] => Array
        (
            [0] => abc
            [1] => bcd
        )

    [1] => Array
        (
            [0] => cde
            [1] => def
        )

    [2] => Array
        (
            [0] => efg
        )

)

Example2

Let's try the same example once again but this time we are going to set parameter preserve_keys to true -

<?php
   $input = array('abc', 'bcd', 'cde', 'def', 'efg');
   print_r(array_chunk($input, 2, true));
?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [0] => Array
        (
            [0] => abc
            [1] => bcd
        )

    [1] => Array
        (
            [2] => cde
            [3] => def
        )

    [2] => Array
        (
            [4] => efg
        )

)

Example3

The following example passes 0 value for the size parameter and it throws a warning message -

<?php
   $input = array('abc', 'bcd', 'cde', 'def', 'efg');
   print_r(array_chunk($input, 0));
?>

Output

When the above code is executed, it will produce the following result -

PHP  Warning:  array_chunk(): Size parameter expected to be greater than 0 in main.php on line 3


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP array_column() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.