How to split an array in php without changing the key value

青灯夜游
Release: 2023-03-12 11:08:01
Original
1785 people have browsed it

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)".

How to split an array in php without changing the key value

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);
Copy after login

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);
?>
Copy after login

Output result:

How to split an array in php without changing the key value

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template