PHP | reset()函数

reset()函数是PHP中的内置函数。

null
  • 此函数用于 将任何数组的内部指针移动到该数组的第一个元素。
  • 在处理数组时,我们可能会使用不同的函数修改数组的内部指针,例如 prev()函数 , current()函数 , key()函数
  • 函数的作用是:重置内部指针,使其指向数组的第一个元素。

语法:

reset($array)

参数: 此函数只接受一个参数 $array 。我们要重置其内部指针以再次指向第一个元素的数组。

返回值 :成功时返回数组的第一个元素,如果数组为空,即数组不包含任何元素,则返回FALSE。

下面的程序演示了PHP中的reset()函数:

项目1:

<?php
// 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:

<?php
// Input array
$arr = array ( 'Delhi' , 'Kolkata' , 'London' );
// getting current element using current()
// function
print current( $arr ). "" ;
// move internal pointer to next element
next( $arr );
print current( $arr ). "" ;
// 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

参考: http://php.net/manual/en/function.reset.php

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享