PHP:如何“砍”我的阵列? [英] PHP: how to 'cut' my array?

查看:84
本文介绍了PHP:如何“砍”我的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)

如何删除最新的2细胞并使其更短?

How can I remove the latest 2 cells and make it shorter ?

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
)

感谢

推荐答案

查看 array_slice()

所以,如果你想要的前三个元素只有:

So, if you wanted the first three elements only:

$array = array_slice($array, 0, 3);

如果你想所有,但最后三个元素:

If you wanted all but the last three elements:

$array = array_slice($array, 0, -3);

第二个参数是起点( 0 表示从数组的开头开始)。

The second parameter is the start point (0 means to start from the begining of the array).

第三个参数是所得阵列的长度。从文档:

The third parameter is the length of the resulting array. From the documentation:

如果的 长度 的,并给出为正,则序列中将具有许多
      在它的元素。如果的 长度 的,并给出为负,那么序列将
      停止从数组末尾的许多元素。如果省略,则
      该序列将拥有一切从的 偏移 的,直到结束时的 阵列

If length is given and is positive, then the sequence will have that many elements in it. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

这篇关于PHP:如何“砍”我的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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