本文将介绍如何使用PHP中的end()函数获取数组中的最后一个元素,并通过示例了解其实现。
end()函数是PHP中的一个内置函数,用于查找给定数组的最后一个元素。end()函数将数组的内部指针指向最后一个元素并返回该元素的值。
语法:
end($array)
参数:该函数接受一个参数$array。它是我们要查找最后一个元素的数组。
返回值:如果数组不为空,则返回数组中最后一个元素的值,否则在失败时返回FALSE。
考虑以下示例。
Input: array('Ram', 'Shita', 'Geeta')
Output: Geeta
Explanation: Here, the input array contain many elements
but output is Geeta i.e, last element of the array
as the end() function returns the last element of an array.
示例1:下面的示例说明了PHP中的end()函数。
<?php
// Input array
$arr = array('Ram', 'Shita', 'Geeta');
// end function print the last
// element of the array.
echo end($arr);
?>
output:
Geeta
示例2:这个示例说明了如何从数组中检索最后一个元素。
<?php
// Input array
$arr = array('1', '3', 'P');
// end function print the last
// element of the array.
echo end($arr)."\n";
// end() updates the internal pointer
// to point to last element as the
// current() function will now also
// return last element
echo current($arr);
?>
output:
P
P