Split an array into array chunks with two elements:
<?php$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");print_r(array_chunk($cars,2));?>The array_chunk() function splits an array into new array chunks.
array_chunk( array , size , preserve_keys );
parameter | describe |
---|---|
array | Required. Specifies the array to use. |
size | Required. An integer specifying how many elements each new array block contains. |
preserve_key | Optional. Possible values: true - retain the key names from the original array. false - default. Each new array block uses a zero-based index. |
Return value: | Returns a multidimensional numeric array, starting from 0 and containing size elements in each dimension. |
---|---|
PHP version: | 4.2+ |
Split an array into two-element array chunks, preserving the keys from the original array:
<?php$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");print_r(array_chunk($ age,2,true));?>