PHP array_chunk Function

In this programming tutorial, you will learn how to work with the PHP array_chunk function.

This function takes an existing array and breaks it up into two or more arrays.

Syntax:

array(array, array size, true/false)

The array signifies the array you wish to use.

The size is the number of elements that the new arrays will have.

The true/false value is technically known as the preserve_key. It is optional and basically dictates whether or not the keys from the originating array are "preserved" or not.

Programming Samples and Usage:


<?php

$test = array ("a" => "Superman", "b" => "Batman", "c" => "Sandman");

print_r(array_chunk($test,3));

?>

 

The above code would result in:

 

array (
[0] => Array([0] => Superman)
[1] => Array([0] => Batman)
[2] => Array([0] => Sandman)
)


You can also choose to preserve the original array keys, by coding the following:

 

<?php

$test = array ("a" => "Superman", "b" => "Batman", "c" => "Sandman");

print_r(array_chunk($test,3,true));

?>

 

Which would result in:

 

array (
[0] => Array([a] => Superman)
[1] => Array([b] => Batman)
[2] => Array([c] => Sandman)
)

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye