PHP array_chunk() function

PHP array_chunk() function is used to split an array into chunks. The array_chunk() function is PHP built-in function.

PHP array_chunk() function divides array into small parts by specifying length.

Syntax :

array_chunk($array,$length,$preserve_keys);  

Parameter,

$array : Required.  It is type of array parameter.
$length : Required. It is size of ech chunk aray.
$preserve_keys : Optional. If it is true, keys will be preserved. Default is FALSE which will re-index the chunk numerically.

 

Let's see below example to understand php array_chunk() Function in details.

Example :

<?php
$tutorials= array('PHP', 'CSS', 'HTML', 'MYSQL', 'LINUX');
echo "<br>preserve_keys is false by default.<br>";
print_r(array_chunk($tutorials, 2));
echo "<br>If preserve_keys is true, keys will be preserved for array.<br>";
print_r(array_chunk($tutorials, 2, true));
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

44330