实现这一目标的最佳方法是什么?
发布于 2010-09-16 01:25:32
您可以将array_slice用作:
$sliced_array = array_slice($array,0,$N);
发布于 2010-09-16 01:25:12
在当前的顺序中?我会说array_slice()。由于它是一个内置函数,它将比循环遍历数组更快,同时跟踪递增的索引,直到N。
发布于 2018-01-29 18:53:24
array_slice()是最好的选择,下面是一些例子:
<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
https://stackoverflow.com/questions/3720096
复制相似问题