php array_chunk() function


  Translation results:

English [tʃʌŋk] US [tʃʌŋk]

n. A thick piece; (something) a considerable amount or part; a strong, strong horse

Plural: chunks

php array_chunk() functionsyntax

Function: Split the array into new array blocks.

Syntax: array_chunk(array,size,preserve_key)

Parameters:

ParameterDescription
array Required. Specifies the array to use.
size Required. An integer value specifying how many elements each new array contains.
preserve_key Optional. Possible values: true - retain the key names from the original array. false - default. Each result array uses a new array index starting from zero.

Description: Split the array into new array blocks. The number of cells in each array is determined by the size parameter. The last array may have a few fewer elements. The optional parameter preserve_key is a Boolean value that specifies whether the elements of the new array have the same keys as the original array (for associative arrays), or new numeric keys starting from 0 (for indexed arrays). The default is to assign new keys.

php array_chunk() functionexample

<?php
$class=array("灭绝","无忌","西门","peter");
print_r(array_chunk($class,2));
?>

Run instance»

Click the "Run instance" button to view the online instance

Output:

Array ( [0] => Array ( [0] => 灭绝 [1] => 无忌 ) [1] => Array ( [0] => 西门 [1] => peter ) )


<?php
$age=array("灭绝"=> 25,"无忌" => 20,"西门" => 27,"peter" => 37);
print_r(array_chunk($age,2,true));
?>

Run Instance»

Click the "Run Instance" button to view the online instance

Output:

Array ( [0] => Array ( [灭绝] => 25 [无忌] => 20 ) [1] => Array ( [西门] => 27 [peter] => 37 ) )

Home

Videos

Q&A