Home >Backend Development >PHP Problem >Can php split an array?

Can php split an array?

WBOY
WBOYOriginal
2022-07-22 14:49:472567browse

Arrays can be split in PHP; arrays can be split using the "array_chunk()" function. This function is used to split an array into new array chunks, that is, to split an array into multiple sub-arrays. , and form these sub-arrays into a multi-dimensional array to return, the syntax is "array_chunk(array,size,preserve_keys);".

Can php split an array?

The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer

php can split the array

The array_chunk() function splits an array into new array chunks.

Syntax

array_chunk(array,size,preserve_keys);

Parameter Description

  • array Required. Specifies the array to use.

  • size Required. An integer specifying how many elements each new array block contains.

  • preserve_key Optional. 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.

The example is as follows:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
print_r(array_chunk($age,2,true));
?>

Output result:

Can php split an array?

##Example 2:

<?php
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
?>

Output result:

Can php split an array?

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Can php 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