Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP array_chunk() Function

PHP array_chunk() Function

PHP Array Reference ManualComplete PHP Array Reference Manual

Example

Split an array into chunks of two elements:

Run Example »


Definition and Usage

The array_chunk() function splits an array into new chunks.


Syntax

array_chunk(

array

,

size

,

preserve_keys

);

Parameters Description
array Required. Specifies the array to use.
size Required. An integer specifying how many elements each new chunk contains.
preserve_key Optional. Possible values:
  • true - Preserves the keys from the original array.
  • false - Default. Each new chunk uses zero-based indexes.
  • |

    Technical Details

    Return Value: Returns a multidimensional numeric array, starting at 0, where each dimension contains size .
    PHP Version: 4.2+

    More examples

    Example 1

    Split an array into chunks of two elements and preserve the original keys:

    "35","Ben"=>"37","Joe"=>"43","Harry"=>"50"); print_r(array_chunk($age,2,true)); ?>

    Run Example »


    PHP Array Reference ManualComplete PHP Array Reference Manual