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

前端 未结 3 1470
甜味超标
甜味超标 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 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.

提交回复
热议问题