php

PHP Array_Chunk() Function

If you want to split your array into chunks of new arrays in an array, array_chunk() is used.

It splits the elements in an array into chunks and places the chunks into new arrays. But these new arrays are placed inside a single actual array.

Let’s see the following syntax:
array_chunk(array_input,chunk_size,preserve_key)

It takes three parameters.

Parameters:

  1. Array_input is the input array that has keys and values.
  2. The second parameter specifies the chunk_size that specifies the array elements to be divided into chunks.
  3. Preserve_key is the optional parameter that is used to retain the actual key names in the chunked arrays. When it sets to true, otherwise, the array names start with 0. By default, it returns 0,1,2….as keys.

Returning Format:

Array
(
  [0] => Array
        (
         [0]=> value 1
         ……..
         ……..
         [n]=>value n
        )

 ………..
………..
 [n] => Array
        (
         [0]=> value 1
         ……..
         ……..
         [n]=>value n
        )

)

Example 1:

In this example, we will create an array that has four key-value pairs and divide them into two chunks by preserving the same keys.

<?php

//create an array named - Flower with four keys and values
$Flower=array('Flower_name1'=>'Lotus','Flower_name2'=>'tulip',
'Flower_name3'=>'lilly','Flower_name4'=>'rose');

//convert the array into 2 chunks by preserving keys
print_r(array_chunk($Flower,2,true));
?>

Output:

We can see that two chunks were returned and the keys were also returned the same, since we specified the third parameter as true.

Example 2:

In this example, we will create an array that has four key-value pairs and divide them into two chunks by not preserving the same keys.

<?php

//create an array named - Flower with four keys and values
$Flower=array("Flower_name1"=>"Lotus","Flower_name2"=>"tulip","Flower_name3"=>
"lilly","Flower_name4"=>"rose");

//convert the array into 2 chunks by not preserving keys
print_r(array_chunk($Flower,2,false));
?>

Output:

We can see that two chunks were returned. Since we specified the third parameter as false, the same keys are not preserved and the keys returned by start with 0.

Example 3:

In this example, we will create an array that has four key-value pairs and divide them into four chunks by preserving the same keys.

<?php

//create an array named - Flower with four keys and values
$Flower=array("Flower_name1"=>"Lotus", "Flower_name2"=> "tulip", "Flower_name3"=>
"lilly", "Flower_name4"=>"rose");

//convert the array into 4 chunks by preserving keys
print_r(array_chunk($Flower,4,true));
?>

Output:

We can see that four chunks were returned and the keys were also returned the same since we specified the third parameter as true.

Example 4:

In this example, we will create an array that has four values and divide them into two chunks.

<?php

//create an array named - Flower with four  values
$Flower=array("Lotus","tulip","lilly","rose");

//convert the array into 2 chunks.
print_r(array_chunk($Flower,2));
?>

Output:

We can see that two values were returned in the first chunk and the remaining values were returned in the second chunk by specifying the keys starting from 0.

Example 5:

In this example, we will create an array that has four values and divide them into three chunks.

<?php

//create an array named - Flower with four  values
$Flower=array("Lotus","tulip","lilly","rose");

//convert the array into 3 chunks.
print_r(array_chunk($Flower,3));
?>

Output:

We can see that three values were returned in the first chunk and the remaining 1 value is in the second chunk by specifying the keys starting from 0.

Conclusion

We learned how to make chunks in the array using the array_chunk() function in PHP. If the array contains key-value pairs, it is possible to preserve the same keys by specifying true in the third parameter of the function. If the array only contains values, the keys are automatically assigned to each value in the chunked array.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain