Getting last 5 elements of a php array

后端 未结 5 1323
清酒与你
清酒与你 2021-01-11 17:04

How can I get the last 5 elements of a PHP array?

My array is dynamically generated by a MySQL query result. The length is not fixed. If the length is smaller or equ

5条回答
  •  天涯浪人
    2021-01-11 17:13

    You need array_slice, which does exactly this.

    $items = array_slice($items, -5);
    

    -5 means "start at five elements before the end of the array".

提交回复
热议问题