Copy the Code code as follows:
$array =array (1,2,3,4,5);
Echo $array [Count ($array) -1];//computes the length of the array, and then gets the last element of the array, if the last element in the array contains a non-numeric key name, the result may not match the expected
Applies to arrays with key names as numbers
Echo ' <br> ';
Echo End ($array);//The inner pointer of the array points to the last cell, for all arrays
Overall, PHP's built-in function end is the best way to do it. We can test it.
PHP is easy to use, can be used loop traversal, class pointers (personally called), but if you go to the last value of the array is also used to traverse the words are not consumed a lot of performance ah??
There are three ways to take the last value of an array better:
?
123456789101112131415 |
<?PHP
$array =
array
(1,2,4,6,8);
echo end
(
$array
);
?>
<?PHP
$array =
array
(1,2,4,6,8);
echo array_pop
(
$array
);
?>
<?PHP
$array =
array
(1,2,4,6,8);
$k =
array_slice
(
$array
,-1,1);
print_r(
$k
);
//结果是一维数组
?>
|
This is the three function of the method of the value, directly valid, select it on Demand
March 31, 2012 Edit: The second method has a disadvantage, the Array_pop () function will be the last number of the original data "out", that is, the equivalent of cutting meaning, the original data will not have the last value
PHP function every day-the last element of the array is taken out