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

Can php split an array?

WBOY
Release: 2023-03-16 15:40:01
Original
2569 people have browsed it

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

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

Output result:

Can php split an array?

##Example 2:

<?php
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
print_r(array_chunk($cars,2));
?>
Copy after login
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!

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