In PHP, you can use the array_chunk() function to split the array. Just set the third parameter of the function to "true", and the key value will not be changed (the original key name will be retained) ;Syntax format "array_chunk ($array,$size,true)".
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 an array, it can split an array into multiple. Syntax format:
array_chunk(array,size,preserve_keys);
Parameter description:
array
represents the array to be divided;
size
indicates the number of elements in the divided sub-array;
preserve_keys
indicates whether to retain the original key names in the arr array, the default is false, that is, not retained, each divided sub-array will use a new numerical 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.
Example: Use the array_chunk() function to split the array without changing the key value
<?php header("Content-type:text/html;charset=utf-8"); $arr = array("name"=>"张三","sex"=>"男","age"=>20,"course"=>"php教程","website"=>"php中文网"); $res = array_chunk($arr,3,true); var_dump($res); ?>
Output result:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to split an array in php without changing the key value. For more information, please follow other related articles on the PHP Chinese website!