开发者

How to get the last n items in a PHP array as another array?

开发者_高级运维

How to I get an array of the last n items of another array in PHP?


$n is equal to the number of items you want off the end.

$arr = array_slice($old_arr, -$n);


You can use array_slice:

$arr = array_slice($old_arr, -$n, $n, true);

If the array indices are meaningful to you, remember that array_slice will reset and reorder the numeric array indices. You need the preserve_keys flag (4th parameter) set to true to avoid this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜