Home  >  Article  >  Backend Development  >  What is the php function to divide an array into segments

What is the php function to divide an array into segments

青灯夜游
青灯夜游Original
2022-08-23 19:44:082052browse

The php function that divides the array into several segments is "array_chunk()". The array_chunk() function can split an array into multiple sub-arrays, and return these sub-arrays into a multi-dimensional array. The number of elements in each divided sub-array is determined by the second parameter of the function; the syntax is "array_chunk() Array, the number of elements in the subarray, whether to retain the key name);".

What is the php function to divide an array into segments

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

Split the array into several segments in php The function is "array_chunk()".

array_chunk() function is used to split an array into new array chunks, that is, to convert a one-dimensional array into a multi-dimensional array.

This function can split an array into multiple sub-arrays and return these sub-arrays into a multi-dimensional array.

array_chunk(array,size,preserve_keys);
Parameters Description
array Required. Specifies the array to use.
size Required. An integer specifying how many elements each new array block contains.
preserve_key

Optional. Indicates whether to retain the original key names in the arr array. The default is false, that is, not retained. Each sub-array after split will use a new numerical index starting from 0; if set to true, the original keys in arr will be retained. name.

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. That is, the number of elements in each subarray is determined by the second parameter size of the function.

Example 1: The parameter size is set to different values

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;); 
$cars=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
var_dump(array_chunk($cars,2));
var_dump(array_chunk($cars,3));
?>

What is the php function to divide an array into segments

It can be seen that each segmented The number of elements in the subarray is determined by size, and the number of elements in the last subarray may be less than size.

Because the third parameter preserve_key is omitted, the key name in the original array is not retained. Let’s take a look at Example 2 without omitting

: Do not omit the parameter preserve_key

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;); 
$cars=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
var_dump(array_chunk($cars,2,true));
var_dump(array_chunk($cars,3,true));
?>

What is the php function to divide an array into segments

Recommended learning: "PHP Video Tutorial

The above is the detailed content of What is the php function to divide an array into segments. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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