array_chunk function in PHP with examples
array_chunk
array_chunk- In this article, you will learn about the PHP array function which is array_chunk function this function divides an array into different blocks of new arrays.
The simple syntax of array_chunk function is
array_chunk(array, size, preserver_key);
the main parameter of this function is the array which is required and that specifies the array to divide into a specified number of a new block of arrays, the second parameter is the size which is additionally required and it is an integer number that specifies the size of each block of new arrays, and last parameter is a preserve key which is discretionary it should be true or false.
I have told you the best way to utilize this function in down to earth way, I have 1 indexed array with name color and in this array have store the distinctive names of colors, now I need to divide this array into a various block of newarray with a fixed size of that new array so I have written one variable chunk_array and I have written array_chunk function with two-parameter, one is the array which is color and second as I have written integer value 2 that implies in each block of new array size will be that implies in each block there 2 sub-array as should be obvious in the underneath code
1 2 3 4 5 6 7 |
<?php $color= array("red", "green", "blue", "white", "black"); $chunk_array= array_chunk($color, 2); echo'<pre>'; print_r($chunk_array); echo'<pre>'; ?> |
Now I want to print this array in the browser so I have written print function with 1 parameter which is chunk_array as you can see in the above code line number 5.
Save this code and run in the browser
You can see that each block has two subarrays and you can also see the keys of each block start with zero.
But suppose I want to divide the array into three arrays in each block and keys number must be in numeric order so in code I have written three instead of two and I have added the third parameter which is true
1 2 3 4 5 6 7 |
<?php $color= array("red", "green", "blue", "white", "black"); $chunk_array= array_chunk($color, 3, true); echo'<pre>'; print_r($chunk_array); echo'<pre>'; ?> |
Save this code and run in the browser
You can see that each block of the array has three sub-arrays in each block and index number of visit numeric order and if not three index each block numerically as you can see in the above figure.
now I want to access each array of the chunk array so I have to write foreach loop with chunk array as you can see in the below code
1 2 3 4 5 6 7 8 9 10 11 |
<?php $color= array("red", "green", "blue", "white", "black"); $chunk_array= array_chunk($color, 3, true); foreach($chunk_array as $row) { foreach($row as $sub_row) { echo $sub_row.'<br />'; } } ?> |
I write foreach loop with variables chunk_array and row and in this block I have again written foreach loop with variable row and sub_row in this block I have written echo statement with sub_array variable. Now save the code and run
I have shown you how to use array_chunk function with the associative of array this is my one associative array with name color, in this array I have written key name as color name and its value is hashed name with a particular color code as you can see below
1 2 3 4 5 6 7 |
$color=array( "red" => "#FF0000", "green" => "#008000", "blue" => "#0000ff", "white" => "#ffffff", "black" => "#000000", ) |
After this I wrote chunk_array variable and wrote chunk_array function with two-parameter one is an array which is color and second is two which is the size of each block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $color=array( "red" => "#FF0000", "green" => "#008000", "blue" => "#0000ff", "white" => "#ffffff", "black" => "#000000", ); $chunk_array=array_chunk($color,2); echo '<pre>'; print_r($chunk_array); echo'</pre>'; ?> |
now I want to print this chunk array so I wrote print with chunk array variable. I saved the code and Run it again.
Now, you can only see the hash name of color but now I want to show the color name so I have written a third parameter which is true and saved the code again
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $color=array( "red" => "#FF0000", "green" => "#008000", "blue" => "#0000ff", "white" => "#ffffff", "black" => "#000000", ); $chunk_array=array_chunk($color,2, true); echo '<pre>'; print_r($chunk_array); echo'</pre>'; ?> |
Now I added the third parameter as you can see in the above code line number 10 which is true
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!