reset()函数是PHP中的内置函数。
此函数用于将任何数组的内部指针移动到该数组的第一个元素。
在处理数组时,可能会使用不同的函数(例如prev()函数,current()函数,key()函数等)来修改数组的内部指针。
reset()函数将内部指针重置为指向数组的第一个元素。
用法:
reset($array)
参数:该函数接受单个参数$array。这是我们要重置其内部指针以再次指向第一个元素的数组。
Return Value:成功返回数组的第一个元素;如果数组为空,则返回FALSE,即数组不包含任何元素。
以下示例程序旨在说明PHP中的reset()函数:
程序1:
// input array
$arr = array('Ram', 'Rahim', 'Geeta', 'Shita');
// here reset() function Moves the internal pointer to the
// first element of the array, which is Ram and also returns
// the first element
$res = reset($arr);
print "$res";
?>
输出:
Ram
程序2:
// Input array
$arr = array('Delhi', 'Kolkata', 'London');
// getting current element using current()
// function
print current($arr)."\n";
// move internal pointer to next element
next($arr);
print current($arr)."\n";
// now reset() is called so that the internal pointer
// moves to the first element again i.e, Delhi.
reset($arr);
print current($arr);
?>
输出:
Delhi
Kolkata
Delhi