Home  >  Article  >  Backend Development  >  What function does php use to split an array?

What function does php use to split an array?

青灯夜游
青灯夜游Original
2022-06-02 15:49:452256browse

php splits the array using the "array_chunk()" function. The array_chunk() function can split an array into multiple sub-arrays with the syntax "array_chunk($array,$size, whether to retain the key name)"; the number of elements in each sub-array is determined by the non-omit parameter "$size".

What function does php use to split an array?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php splits the array using "array_chunk( )"function.

array_chunk() function can split an array into multiple sub-arrays. Its syntax is as follows:

array_chunk($array,$size,$preserve_keys);

Parameter description:

  • $array: Indicates the array to be divided and cannot be omitted;

  • $size: Indicates the number of elements of the divided sub-array, which cannot be omitted;

  • $preserve_keys: Optional, indicating whether to retain the original key names in the arr array. The default is false, that is, not retained. Each sub-array after splitting will use a new numeric index starting from 0; if set to true , then retain the original key name in arr.

The number of elements in each divided subarray is determined by $size. The last subarray may contain 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");
var_dump(array_chunk($cars,2));
var_dump(array_chunk($cars,3));
?>

What function does php use to split an array?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What function does php use to split an array?. 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