Free as Freedom, not Free as Free Pizza!” />

-

Udruženje Informatičara BiH - Cross-platform and Portable Development With PHP, LAMP/WAMP development, AJAX and Javascript, ASP, dot NET, C sharp, C++, C, VB, Oracle, tutorials and tips...
Free as Freedom, not Free as Free Pizza!


 
Web http://www.uibih.co.nr

Add to Google

Wednesday, October 18, 2006

PHP array_chunk() Function

array_chunk -- splits the array into several arrays
with size values in them. You may also have an array with less values at the
end. You get the arrays as members of a multidimensional array indexed with
numbers starting from zero.




Syntax:

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


It returns an array, recieves an array,an integer and a boolean value for parameters,
the array is our array we need to chunk, the integer is number of elements in
every chunk, and the boolean is true or false, to presrve keys or not.


A simple example presenting this function:


Example 1:

<?php

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

print_r(array_chunk($input_array, 2));

print_r(array_chunk($input_array, 2, true));

?>


Output:

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

)


)