PHP Executing Upon an Array Split array into chunks


array_chunk() splits an array into chunks

Let's say we've following single dimensional array,

$input_array = array('a', 'b', 'c', 'd', 'e');

Now using array_chunk() on above PHP array,

$output_array = array_chunk($input_array, 2);

Above code will make chunks of 2 array elements and create a multidimensional array as follow.

    [0] => Array
            [0] => a
            [1] => b

    [1] => Array
            [0] => c
            [1] => d

    [2] => Array
            [0] => e


If all the elements of the array is not evenly divided by the chunk size, last element of the output array will be remaining elements.

If we pass second argument as less then 1 then E_WARNING will be thrown and output array will be NULL.

$array (array)Input array, the array to work on
$size (int)Size of each chunk ( Integer value)
$preserve_keys (boolean) (optional)If you want output array to preserve the keys set it to TRUE otherwise FALSE.