Split array into chunks

suggest change

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.

Array
(
    [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.

Parameter | Details | ——— | —–– | $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. |

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Arrays:
* Arrays
* Split array into chunks

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR