If you have a single-dimensional array, and want to break it into chunks, you can use the `array_chunks` method.

It takes 3 arguments:

1. The input array / original array
2. The length of the new array (how many dimensions to create)
3. A boolean to preserve the original keys or not

`<strong>array_chunk</strong>(array&nbsp;\$array,&nbsp;int&nbsp;\$length,&nbsp;bool&nbsp;\$preserve_keys=<strong>false</strong>&nbsp;):array`

## An example of dividing an array into chunks

Let’s take an initial starter array with 5 values

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

If we said we would like to create 2 chunks:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 `````` ``````\$new_array = array_chunk(\$input_array, 2); print_r(\$new_array); /* Array (  => Array (  => a  => b )  => Array (  => c  => d )  => Array (  => e ) ) */ ``````

So now let’s try it with preserving the keys:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 `````` ``````\$new_array = array_chunk(\$input_array, 2, true); print_r(\$new_array); /* Array (  => Array (  => a  => b )  => Array (  => c  => d )  => Array (  => e ) ) */ ``````

## But where would I use this?

This is particularly helpful when constructing tables into HTML renders, or when needing to populate rows and columns when using Bootstrap css `col-x-x` blocks:

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 `````` ``````\$values = range(1, 31); \$rows = array_chunk(\$values, 7); print "\n"; foreach (\$rows as \$row) { print "
" . \$value . "
\n"; foreach (\$row as \$value) { print "\n"; } print "\n"; } print "\n"; ``````