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

前端 未结 3 1457
甜味超标
甜味超标 2020-12-09 15:12

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

3条回答
  •  失恋的感觉
    2020-12-09 15:40

    You can use http://us2.php.net/array_slice.

    $new = array_slice($old, $n);
    

    However, $n is the offset to start the slice, so to calculate it, you would need to subtract this from the length of the array: $n = count($old) - $target_size.

提交回复
热议问题