如何使用php将数组拆分为两个相等的部分 [英] how to split the array in to two equal parts using php

查看:215
本文介绍了如何使用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.

这篇关于如何使用php将数组拆分为两个相等的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆