PHP Programing language

array_chunk() Function in PHP
Previous Home Next
adplus-dvertising

array_chunk()

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

Syntax:
array_chunk(array,size,preserve_key);

In above syntax, "array" specifies the array to use, "size" is an integer that specifies the size of each chunk and "preserve_key" preserve_key values are as follows:

  • true: Preserves the keys
  • false: Default and reindexes the chunk numerically

Example:

<?php
$fruit_weight=array("Apple"=>"5kg","Banana"=>"7kg","Mango"=>"10kg");
print_r(array_chunk($fruit_weight,2));
?>

Output:

Array ( [0] => Array ( [0] => 5kg [1] => 7kg ) [1] => Array ( [0] => 10kg ) )

Previous Home Next