Method 1: Use PHP built-in function end()
end()
The function points the internal pointer to the array the last element and output it.
<?PHP $array = array(1,2,4,6,8); echo end($array); ?>
More learning video recommendations: Getting started with php development
Method 2: Use the function array_pop()
array_pop()
Function deletes the last element in the array.
<?PHP $array = array(1,2,4,6,8); echo array_pop($array); ?>
Method 3: Use the function array_slice()
array_slice()
The function returns the selected part of the array.
Note: If the array has string key names, the returned array will retain the key names.
<?PHP $array = array(1,2,4,6,8); $k = array_slice($array,-1,1); print_r($k); //结果是一维数组 ?>
Recommended related articles and tutorials: php tutorial
The above is the detailed content of How to get the last element in an array in php. For more information, please follow other related articles on the PHP Chinese website!