PHP - Function array_chunk()


Advertisements


Syntax

array array_chunk ( array $input, int $size [, bool $preserve_keys] );

Definition and Usage

It chunks an array into size large chunks. The last chunk may contain less than size elements.

Parameters

Sr.No Parameter & Description
1

input

The array to work on

2

size

The size of each chunk

3

preserve_keys

When set to TRUE keys will be preserved. Default is FALSE which will reindex the chunk numerically

Return Values

It returns a multidimensional numerically indexed array

Example

Try out following example −

<?php
   $input = array('abc', 'bcd', 'cde', 'def', 'efg');
   print_r(array_chunk($input, 4));
   print_r(array_chunk($input, 2, true));
?> 

This will produce following result −

Array ( [0] => Array ( [0] => abc [1] => bcd [2] => cde [3] => def ) [1] => Array ( [0] => efg ) ) Array ( [0] => Array ( [0] => abc [1] => bcd ) [1] => Array ( [2] => cde [3] => def ) [2] => Array ( [4] => efg ) )

php_function_reference.htm

Advertisements