array_chunk
ALLOWS to obtain a new MULTIDIMENSIONAL ARRAY, from an INITIAL ARRAY through its sectioning.
This function chunks an ARRAY into arrays with $size elements.
The last chunk may contain less than $size elements.
When $preserve_keys = TRUE the keys will be preserved.
When $preserve_keys = FALSE the keys will be reindexed numerically.
This function returns multidimensional numerically indexed ARRAY, starting with ZERO, with each dimension containing $size elements.
If $size < 1 an E_WARNING will be thrown and NULL returned.
<?php
arr array_chunk ( arr $array , int $size [, bool $preserve_keys = FALSE ] )
where,
$array = The input ARRAY
$size = The size of each chunk
$preserve_keys = To control if the keys will be preserved or not
( SEE the below TABLE )
?>
$array
The input ARRAY.
$size
The size of each chunk.
$preserve_keys
To control if the keys will be preserved or not.
VALUE |
WHAT DOES |
FALSE |
The keys WILL NOT BE preserved |
TRUE |
The keys WILL BE preserved |
ed48 |
EXERCISE
<?php
$arr01 = [ "RGB" => ["RED", "GREEN", "BLUE"],
"HSK" => [ "HUE", "SATURATION", "BLACK"] ];
echo 'The given ARRAY:<br><pre>';
print_r($arr01);
echo '</pre><br><br>';
$sct01 = 1;
echo 'Number of cunks: ' . $sct01 . '<br><br>';
$prk01f = FALSE;
$prk01t = TRUE;
echo 'Preserved keys: FALSE';
echo '<br>Resulting array:<br>';
$ckd01f = array_chunk($arr01, $sct01, $prk01f);
echo '<pre>';
var_dump($ckd01f);
echo '</pre><br><br>Preserved keys: TRUE';
echo '<br>Resulting array:<br><pre>';
$ckd01t = array_chunk($arr01, $sct01, $prk01t);
var_dump($ckd01t);
echo '</pre>';
?>
RESULT
The given ARRAY:
Array
(
[RGB] => Array
(
[0] => RED
[1] => GREEN
[2] => BLUE
)
[HSK] => Array
(
[0] => HUE
[1] => SATURATION
[2] => BLACK
)
Number or chunks: 1
Without preserving the keys:
array(2) {
[0]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(3) "RED"
[1]=>
string(5) "GREEN"
[2]=>
string(4) "BLUE"
}
}
[1]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(3) "HUE"
[1]=>
string(10) "SATURATION"
[2]=>
string(5) "BLACK"
}
}
}
With key preservation:
array(2) {
[0]=>
array(1) {
["RGB"]=>
array(3) {
[0]=>
string(3) "RED"
[1]=>
string(5) "GREEN"
[2]=>
string(4) "BLUE"
}
}
[1]=>
array(1) {
["HSK"]=>
array(3) {
[0]=>
string(3) "HUE"
[1]=>
string(10) "SATURATION"
[2]=>
string(5) "BLACK"
}
}
}
EXERCISE
<?php
$arr02 = [ 'R' => "RED", 'G' => "GREEN", 'B' => "BLUE",
'Y' => "YELLOW", 'M' => "MAGENTA", 'C' => "CYAN",
'K' => "BLACK" ];
echo 'The given ARRAY:<br><pre>';
print_r($arr02);
echo '</pre><br><br>';
$sct02 = 3;
echo 'Number of cunks: ' . $sct02 . '<br><br>';
$prk02f = FALSE;
$prk02t = TRUE;
echo 'Preserved keys: FALSE';
echo '<br>Resulting array:<br>';
$ckd02f = array_chunk($arr02, $sct02, $prk02f);
echo '<pre>';
var_dump($ckd02f);
echo '</pre><br><br>Preserved keys: TRUE';
echo '<br>Resulting array:<br><pre>';
$ckd02t = array_chunk($arr02, $sct02, $prk02t);
var_dump($ckd02t);
echo '</pre>';
?>
EXERCISE
<?php
$arr03 = [ 'R' => "RED", 'G' => "GREEN", 'B' => "BLUE",
'Y' => "YELLOW", 'M' => "MAGENTA", 'C' => "CYAN",
'K' => "BLACK" ];
$sct03 = -3;
$prk03f = FALSE;
$prk03t = TRUE;
$ckd03f = array_chunk($arr03, $sct03, $prk03f);
var_dump($ckd03f);
echo '<br><br>';
$ckd03t = array_chunk($arr03, $sct03, $prk03t);
var_dump($ckd03t);
?>
EXERCISE
<?php
echo "Testing basic functionality:<br><br>";
$size = 2;
$input_arrays = array (
// array with default keys - numeric values
array(1, 2, 3, 4, 5),
// array with default keys - string values
array('value1', "value2", "value3"),
// associative arrays - key as string
array('key1' => 1, "key2" => 2, "key3" => 3),
// associative arrays - key as numeric
array(1 => 'one', 2 => "two", 3 => "three"),
// array containing elements with/without keys
array(1 => 'one','two', 3 => 'three', 4, "five" => 5)
);
echo 'The given ARRAY:<br>';
print_r($input_arrays);
$count = 1;
// loop through each element of the array for input
foreach ($input_arrays as $input_array){
echo "<br><br>Iteration: $count<br>";
var_dump( array_chunk($input_array, $size) );
$count++;
}
?>
EXERCISE
<?php
echo "Testing basic functionality:<br><br>";
$size = 2;
$input_arrays = array(
// array with default keys - numeric values
array(1, 2, 3, 4, 5),
// array with default keys - string values
array('value1', "value2", "value3"),
// associative arrays - key as string
array('key1' => 1, "key2" => 2, "key3" => 3),
// associative arrays - key as numeric
array(1 => 'one', 2 => "two", 3 => "three"),
// array containing elements with/without keys
array(1 => 'one','two', 3 => 'three', 4, "five" => 5)
);
echo 'The given ARRAY:<br>';
print_r($input_arrays);
$count = 1;
// loop through each element of the array for input
foreach ($input_arrays as $input_array){
echo "<br><br>Iteration: $count<br>";
var_dump( array_chunk($input_array, $size, true) );
echo '<br><br>';
var_dump( array_chunk($input_array, $size, false) );
$count++;
}
?>
EXERCISE
<?php
$array = array (1 => 1, 2 => 2, 3 => 3);
var_dump ($array);
echo '<br><br>';
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>Size: $i<br>";
var_dump (array_chunk ($array, $i));
echo '<br><br>';
var_dump (array_chunk ($array, $i, TRUE));
echo '<br><br>';
var_dump (array_chunk ($array, $i, FALSE));
}
?>
EXERCISE
<?php
$array = array (0 => 0, 3 => 2);
echo 'The given ARRAY:<br>';
var_dump ($array);
echo '<br><br>';
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "[$i]<br>";
var_dump (array_chunk ($array, $i));
echo '<br><br>';
var_dump (array_chunk ($array, $i, TRUE));
echo '<br><br>';
var_dump (array_chunk ($array, $i, FALSE));
echo "<br><br>";
}
?>
EXERCISE
<?php
$array = array (1 => 1, 5 => 2, 8 => 3);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array (1, 2);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array (0, 1, 2);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array (1, 2, 3);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array (0, 1, 2, 3);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array (1, 2, 3, 4);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array (0, 1, 2, 3, 4);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
var_dump (array_chunk ($array, $i, TRUE));
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = array ("a" => 1);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
echo "<br>";
var_dump (array_chunk ($array, $i, TRUE));
echo "<br>";
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = ["p" => 1, "q" => 2,
"r" => 3, "s" => 4,
"u" => 5, "v" => 6];
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
echo "<br>";
var_dump (array_chunk ($array, $i, TRUE));
echo "<br>";
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array = ["p" => "A", "q" => "B",
"r" => "C", "s" => "D",
"u" => "E", "v" => "F"];
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
echo "<br><br>";
var_dump (array_chunk ($array, $i, TRUE));
echo "<br><br>";
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
$array =array (0);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
echo "<br><br>";
var_dump (array_chunk ($array, $i, TRUE));
echo "<br><br>";
var_dump (array_chunk ($array, $i, FALSE));
echo "<br>";
}
?>
EXERCISE
<?php
/*
* Testing array_chunk() function -
* input array containing different sub arrays
*/
echo "Testing usage variations:<br><br>";
$size = 2;
// input array
$input_array = array (
"array1" => array(),
"array2" => array(1, 2, 3),
"array3" => array(1)
);
echo 'The given ARRAY:<br>';
var_dump($input_array);
echo "<br><br>Testing array_chunk()
by supplying an array containing
different sub arrays & 'preserve_key' as default:<br>";
var_dump( array_chunk($input_array, $size) );
echo "<br><br>Testing array_chunk()
by supplying an array containing
different sub arrays & 'preserve_key' = true:<br>";
var_dump( array_chunk($input_array, $size, true) );
echo "<br><br>Testing array_chunk()
by supplying an array containing
different sub arrays & 'preserve_key' = false:<br>";
var_dump( array_chunk($input_array, $size, false) );
?>
EXERCISE
<?php
/*
* Testing array_chunk() function with
* following conditions
* 1. array without elements
* 2. associative array with duplicate keys
* 3. array with one element
*/
echo "Testing array_chunk() : usage variations:<br><br>";
// input array
$input_arrays = array (
// array without elements
"array1" => array(),
// array with one element
"array2" => array(1),
// associative array with duplicate keys
"array3" => array("a" => 1, "b" => 2, "c" => 3, "a" => 4, "d" => 5)
);
echo 'The given ARRAY:<br>';
var_dump($input_arrays);
$size = 2;
$count = 1;
echo "<br><br>Size: $size<br><br>Count: $count<br><br>";
echo "Testing by supplying various arrays:<br>";
// loop through the array for 'array' argument
foreach ($input_arrays as $input_array){
echo "<br><br>Iteration: $count<br>";
var_dump( array_chunk($input_array, $size) );
echo '<br><br>';
var_dump( array_chunk($input_array, $size, true) );
echo '<br><br>';
var_dump( array_chunk($input_array, $size, false) );
$count++;
}
?>
EXERCISE
<?php
/*
* Testing array_chunk() function
* with following conditions
* 1. input array containing references
*/
echo "Testing usage variations:<br><br>";
$size = 2;
echo "Testing input array containing references:<br><br>";
$numbers=array(1, 2, 3, 4);
// reference array
$input_array = array (
"one" => &$numbers[0],
"two" => &$numbers[1],
"three" => &$numbers[2],
"four" => &$numbers[3]
);
echo 'Reference ARRAY:<br>';
var_dump($numbers);
echo '<br><br>The given ARRAY:<br>';
var_dump($input_array);
echo "<br><br>Size: $size<br><br>";
var_dump( array_chunk($input_array, $size) );
echo '<br><br>';
var_dump( array_chunk($input_array, $size, true) );
echo '<br><br>';
var_dump( array_chunk($input_array, $size, false) );
?>
EXERCISE
<?php
$array = array (3 => -1);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
echo '<br><br>';
var_dump (array_chunk ($array, $i, TRUE));
echo '<br><br>';
var_dump (array_chunk ($array, $i, FALSE));
}
?>
EXERCISE
<?php
$array = array (1 => 0, 2 => 2);
echo 'The given ARRAY:<br>';
var_dump ($array);
for ($i = 1; $i < (sizeof($array) + 1); $i++) {
echo "<br><br>[$i]<br>";
var_dump (array_chunk ($array, $i));
echo '<br><br>';
var_dump (array_chunk ($array, $i, TRUE));
echo '<br><br>';
var_dump (array_chunk ($array, $i, FALSE));
}
?>