如何在 PHP 中使用 array_slice() 将数组分成两个相等的部分?
这是我的要求:
第一个数组包含:0-1200
第二个数组包含:1200-end
最佳答案
来自 the documentation for array_slice ,您所要做的就是给 array_slice
一个偏移量和一个长度。
在你的情况下:
$firsthalf = array_slice($original, 0, 1200);
$secondhalf = array_slice($original, 1200);
换句话说,这段代码告诉array_slice
:
take the first 1200 records;
then, take all the records starting at index 1200;
由于索引 1200 是项目 1201,这应该是您需要的。
关于php - 如何使用php将数组分成两个相等的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5561952/