Home > Article > Backend Development > How to split an array into parts in php
In PHP, you can use the array_chunk() function to split an array into several parts. This function can split an array into multiple sub-arrays, and return these sub-arrays into a multi-dimensional array; syntax "array_chunk ($arr,$size,$preserve_keys)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use the array_chunk() function to split the array into parts.
array_chunk() function can split an array into multiple ones. Its syntax is as follows:
array array_chunk ( array $arr , int $size [, bool $preserve_keys = false ] )
Parameter description:
arr represents the number to be split Array;
size represents the number of elements in the divided sub-array;
preserve_keys represents whether to retain the original key names in the arr array. The default is false, that is, it is not retained. Each sub-array after splitting will use a new numeric index starting from 0; if set to true, the original key names in arr will be retained.
array_chunk() will split the arr array into multiple sub-arrays, and the number of elements in each sub-array is determined by size. The last subarray may have less than size elements.
Return value: Returns a multi-dimensional array composed of divided sub-arrays.
Example:
<?php $cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel"); $chunk=array_chunk($cars,2); var_dump($chunk); ?>
Output result:
PHP Video Tutorial"
The above is the detailed content of How to split an array into parts in php. For more information, please follow other related articles on the PHP Chinese website!