Archive

Posts Tagged ‘array_chunk’

PHP:: How to split a large array in small pieces :: or split array into chunks

April 29th, 2009 R Arun Raj 1 comment

Hello All,
Yesterday i was searching for a solution to split large array into small pieces. Suddenly i noticed the that php have a built in function to split array into small pieces. It was very surprise to me ..
Here it is
Function : array array_chunk ( array $input , int $size [, bool $preserve_keys= false ] )

< ?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>

The output will be like this : –

Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => c
[1] => d
)

[2] => Array
(
[0] => e
)

)
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[2] => c
[3] => d
)

[2] => Array
(
[4] => e
)

)

Have fun Cheers

Categories: PHP, email Tags: , ,