Php Array Elements Split/ Chunk Function
What is array_chunk Function?
Explanation
The "array_chunk" function is used to split an array elements into chunks.
Syntax:
array_chunk(array,size,preserve_key)
In the above syntax the parameter "array" specifies the array, "size" specifies the size of each chunk, "preserve_key" preserves the keys from the original array if "true", that is the keys will not change it will be same as in the original array.
Example :
<?php
$input_array = array('a', 'b', 'c', 'd');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
Result :
Array
(
[0] => Array
(
[0] => a [1] => b
)
[1] => Array
(
[0] => c [1] => d
)
)
Array
(
[0] => Array
(
[0] => a [1] => b
)
[1] => Array
(
[2] => c [3] => d
)
)
)
In the above example first array_chunk function split elements and returns the keys without preserving the values that is the keys are "0", "1", but the second function returns keys as same in the array that is "0", "1" , "2" and "3".