array_chunk ( ) – Split an array into specified size chunks

The array_chunk() PHP array function will chunk an array at a specified size. It takes up to three parameters. The first parameter we feed it is the target array. The second parameter is the size you wish your chunks to be. And the third parameter is optional. Parameter three uses values of either “true” or “false”, when used you can choose to preserve existing keys in the array. If you do not use the third parameter this function defaults to reindexing the array just as if you specify “false” for the third parameter. Specify a value of “true” as the third parameter if you wish to preserve the keys in the array, and use “false”(or omit param 3) to reindex the array.

Example

 <?php $array = array("1","2","3","4","5","6","7","8","9"); ?>
<?php $values = array_chunk($array, 4); // inbuilt function (PHP 4 >= 4.2.0, PHP 5) ?>
<?php foreach($values as $val): ?>
<div>
<ul>
<?php foreach($val as $v): ?>
<li><?php echo $v; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>

Output

   . 1
   . 2
   . 3
   . 4

   . 5
   . 6
   . 7
   . 8

   . 9