Home > Article > Backend Development > Can php split an array?
Arrays can be split in PHP; arrays can be split using the "array_chunk()" function. This function is used to split an array into new array chunks, that is, to split an array into multiple sub-arrays. , and form these sub-arrays into a multi-dimensional array to return, the syntax is "array_chunk(array,size,preserve_keys);".
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
The array_chunk() function splits an array into new array chunks.
Syntax
array_chunk(array,size,preserve_keys);
Parameter Description
array Required. Specifies the array to use.
size Required. An integer specifying how many elements each new array block contains.
preserve_key Optional. Possible values:
#true - Keep the key names from the original array.
false - Default. Each new array block uses a zero-based index.
Return value: Returns a multi-dimensional numerical array, starting from 0, and each dimension contains size elements.
The example is as follows:
<?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50"); print_r(array_chunk($age,2,true)); ?>
Output result:
<?php $cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel"); print_r(array_chunk($cars,2)); ?>Output result: Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of Can php split an array?. For more information, please follow other related articles on the PHP Chinese website!