gpt4 book ai didi

PHP:如何将我的数组设为 'cut'?

转载 作者:IT王子 更新时间:2023-10-28 23:59:53 25 4
gpt4 key购买 nike

我有一个数组

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

如何删除最新的 2 个单元格并使其更短?

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

谢谢

最佳答案

查看 array_slice()

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

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

如果你想要除了最后三个元素之外的所有元素:

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

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

第三个参数是结果数组的长度。来自文档:

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:如何将我的数组设为 'cut'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3585966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com