Getting last 5 elements of a php array

后端 未结 5 1324
清酒与你
清酒与你 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:30

    It's simple use array_slice and count()

    $arraylength=count($array);
    
        if($arraylength >5)
             $output_array= array_slice($array,($arraylength-5),$arraylength);
        else
            $output_array=$array;
    

提交回复
热议问题