PHP

array_chunk function in PHP with examples

array_chunk

Description:

array_chunk :-hello friends in this article I will going to learn you one more PHP array function array_chunk function this function divide an array into a different block of new arrays.

syntax of  array_chunk function:

array_chunk(array, size, preserver_key);

the first parameter of this function which is required and that specifies the disarray to divide into a specified number of a new block of arrays second parameter is the size which is also 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 optional it must true or false.

now friends I have shown you how to use this function in a practical way I have 1 indexed array with name color and in this array have store the different name of color now I want to divide this array into a different block of newarray with fixed size of that new array so I have write one variable chunk_array and I have write array_chunk function with two-parameter one is the array which is color and second as I have write integer value 2 that means in each block of new array size will be that means in each block there 2 sub-array as you can see in the below code

now I want to print this array in browser so I write a 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

array_chunk

friends you can see that each block has two subarray and you can also see the keys of each block starts by zero.

but suppose I want to divide the array into three arrays in each block and keys number must in numeric order so in code I have write three and replace of two and I have added the third parameter which is true

saved this code and run in the browser

array_chunk

friends you can see that each block of the array has three sub-array in each block and index number of visit in 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 write foreach loop with chunk array as you can see in the below code

I write foreach loop with variables chunk_array and row and in this block I have again write foreach loop with variable row and sub_row in this block I have write echo statement with sub_array variable. Now save the code and run

array_chunk

now friends 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 write key name as color name and its value is hashed name with a particular color code as you can see below

$color=array(

“red” => “#FF0000”,

“green” => “#008000”,

“blue” => “#0000ff”,

“white” => “#ffffff”,

“black” => “#000000”,

)

After this I have write chunk_array  variable and write chunk_array function with two parameter one is array which is color and second is two which is size of each block

now I want to print this chunk array so I have write print with chunk array variable.


Now  save the code and run

array_chunk

Friends you can only see the hash name of color but now I want to show the color name so I have write  third parameter is true and saved the code again

Now I add the third parameter as you can see in the above code line number 10 which is true

array_chunk

now friends you can see that color name with hash code.

 

Related Articles

Leave a Reply

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

Back to top button