Method 1:
<?PHP $array = array(1,2,4,6,8); echo end($array); ?>
Output
//输出8
Method 2:
<?PHP $array = array(1,2,4,6,8); echo array_pop($array); ?>
Output
//输出8
Method 3:
<?PHP $array = array(1,2,4,6,8); $k = array_slice($array,-1,1); print_r($k); //结果是一维数组 ?>
Output
Array ( [0] => 8 )
The second method has a drawback. The Array_pop() function will "take out" the last number of the original data, which is equivalent to cutting. It turns out There will no longer be a last value in the data. Re re:
<?PHP $array = array(1,2,4,6,8); array_pop($array); print_r($array); ?>
Output:
Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 6 )
is a built -in function in PHP. In fact, we can also obtain the PHP array through some of the methods we write The last element in
<?php $array = array(1,2,4,6,8); $last = count($array); echo $array[$last-1]; ?>