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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You can use
array_slice
orarray_splice
:Note that
array_slice
copies the items of$a
and returns them whilearray_splice
does modify$a
itself and only returns the items that have been removed from$a
.