Is there a way to only keep the first N (for example 10) elements of an array? I know there is array_pop
, but is there a better, more elegant way?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use array_slice
or array_splice
:
$b = array_slice($a, 0, 10);
$c = array_splice($a, 0, 10);
Note that array_slice
copies the items of $a
and returns them while array_splice
does modify $a
itself and only returns the items that have been removed from $a
.