Definition and usage
The Array_chunk () function splits an array into a new array block.
The length of each array is determined by the parameter size.
Optional parameter Preserve_key is a Boolean value that specifies whether the new array uses the same key (index) as the original array, or the key (index) to reassign. The default is to reassign the new key.
Grammar
1 array_chunk (Array,size,preserve_key)
| Parameters |
Describe |
| Array |
Necessary. Specifies the array to use. |
| Size |
Necessary. Specifies the length of each new array. |
| Preserve_key |
Optional. Possible values:
- True-preserves the key names in the original array.
- False-Default. Each result array uses a new zero-based index of the array.
|
Example 1
<?php$a = Array ("A" = "a", "B" = "B", "C" = "C", "D" = "D", "e" = "E");p Rint_r (Array_chunk ($a, 2));? >
Output:
Array ([ 0] = = Array ([0] = A [1] = B) [1] = = Array ([0] = = C [1] + D) [2] + = Array ( [0] = (E)))
Example 2
<?php$a = Array ("A" = "a", "B" = "B", "C" = "C", "D" = "D", "e" = "E");p Rint_r (Array_chunk ($a, 2,true));? >
Output:
Array ( [0] = = Array ([a] + a [b] = = b) [1] = = Array ([c] + = c [d] + D) [2] = = Array ([E] = e))
PHP Array_chunk ()