array_chunk
Splits an array into chunks of specified size, returning a multidimensional indexed array with each chunk containing the specified number of elements.
Parameters
main.php
Return Values
array
Returns multidimensional array where each dimension contains length elements
On success:Array of arrays, each containing up to 'length' elements from the original array
On failure:Empty array if input array is empty, warning if length is less than 1
Notes
- Last chunk may contain fewer elements than specified length
- By default, original keys are not preserved - set preserve_keys to true to maintain them
- Chunk size must be greater than 0, otherwise generates warning
- Useful for pagination, data processing in batches, and display formatting
Changelog
- 4.2.0: Function was introduced
- 4.2.0: preserve_keys parameter added
Related Functions
explode
StringSplits a string by a delimiter and returns an array of strings.
splitparsecsv