Home arrow PHP arrow PHP array_chunk Function

PHP array_chunk Function

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

By: TheloniusFunk
Rating: starstarstarstarstar / 0
April 04, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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)
)


 
 
>>> More PHP Articles          >>> More By TheloniusFunk
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: