PHP array_chunk() Function

The array_chunk() function splits an array into inside of array.

The last chunk may contain less than size elements.

The array_chunk() function splits an array into chunks and stores the chunks into new arrays.

Parameter Description
array Required. Specifies the array to use
size Required. An integer that specifies the size of each chunk
preserve_key Optional. Possible values: true – Preserves the keys, false – Default. Reindexes the chunk numerically
$name = array("Prince","Alex","James","Lachlan","Samuel","Cooper");
print_r(array_chunk($name,2));

Output

Array (
       [0] => Array 
              ( 
                [0] => Prince 
                [1] => Alex 
              ) 
       [1] => Array 
              ( 
                [0] => James 
                [1] => Lachlan 
              ) 
       [2] => Array 
              ( 
                [0] => Samuel 
                [1] => Cooper 
              ) 
      ) 

Leave a Reply

Your email address will not be published. Required fields are marked *