如何使用php将数组拆分为两个相等的部分

问题描述:

如何在PHP中使用array_slice()将数组分为两个相等的部分?

how to split an array in to two equal parts using array_slice() in PHP ?

第一个数组包含:0-1200

First array contains: 0-1200

第二个数组包含:1200端

Second array contains: 1200-end

来自 array_slice的文档,您要做的就是给array_slice一个偏移量和一个长度.

From the documentation for array_slice, all you have to do is give array_slice an offset and a length.

在您的情况下:

$firsthalf = array_slice($original, 0, 1200);
$secondhalf = array_slice($original, 1200);

换句话说,这段代码告诉array_slice:

In other words, this code is telling array_slice:

take the first 1200 records;
then, take all the records starting at index 1200;

由于索引1200是项目1201,因此这应该是您所需要的.

Since index 1200 is item 1201, this should be what you need.