array_slice不返回最后的值

时间:2014-05-05 00:36:21

标签: php

我不明白为什么它不能在我的机器上工作:

$texts = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
$whatever = array_slice($texts, 0, -3, true);
dd($whatever);

这会返回“a,b,c,d”,但我希望它返回“e,f,g”。

如果我写

$whatever = array_slice($texts, 0, 4, true);

这也返回“a,b,c,d”

我想要的只是返回“e,f,g”and the manual examples似乎无法在我的机器上运行。

编辑:我刚看到,如果我这样做:

 $whatever = array_slice($texts, -3);

我将获得数组的最后3个元素,但顺序错误。我希望最后一个数组是第一个。我想我需要反转数组,还是有另一种方式?

1 个答案:

答案 0 :(得分:5)

第二个参数是偏移量,第三个参数是长度,所以理想情况下你想做:

$whatever = array_slice($texts, -3, 3, true)

修改 刚看到你需要它倒退......

$whatever = array_reverse(array_slice($texts, -3, 3, true))