zl程序教程

您现在的位置是:首页 >  后端

当前栏目

php如何获取数组第一个值

PHP数组 如何 获取 第一个
2023-06-13 09:15:03 时间

reset(PHP3,PHP4,PHP5)
reset--将数组的内部指针指向第一个单元

说明
mixedreset(array&array)
reset()将array的内部指针倒回到第一个单元并返回第一个数组单元的值,如果数组为空则返回FALSE。

例1.reset()例子

复制代码代码如下:

<?php
 $array=array("stepone","steptwo","stepthree","stepfour");
 //bydefault,thepointerisonthefirstelement
 echocurrent($array)."<br/>\n";//"stepone"
 //skiptwosteps
 next($array);
 next($array);
 echocurrent($array)."<br/>\n";//"stepthree"
 //resetpointer,startagainonstepone
 reset($array);
 echocurrent($array)."<br/>\n";//"stepone"
 ?>

next(PHP3,PHP4,PHP5)
next-- 将数组中的内部指针向前移动一位

说明
mixednext(array&array)
返回数组内部指针指向的下一个单元的值,或当没有更多单元时返回FALSE。

next()和current()的行为类似,只有一点区别,在返回值之前将内部指针向前移动一位。这意味着它返回的是下一个数组单元的值并将数组指针向前移动了一位。如果移动指针的结果是超出了数组单元的末端,则next()返回FALSE。

警告
如果数组包含空的单元,或者单元的值是0则本函数碰到这些单元也返回FALSE。要正确遍历可能含有空单元或者单元值为0的数组,参见each()函数。

例1.next()及相关函数的用法示例

复制代码代码如下:

<?php
 $transport=array("foot","bike","car","plane");
 $mode=current($transport);//$mode="foot";
 $mode=next($transport);  //$mode="bike";
 $mode=next($transport);  //$mode="car";
 $mode=prev($transport);  //$mode="bike";
 $mode=end($transport);   //$mode="plane";
 ?>

current(PHP3,PHP4,PHP5)
current--返回数组中的当前单元

说明
mixedcurrent(array&array)

每个数组中都有一个内部的指针指向它“当前的”单元,初始指向插入到数组中的第一个单元。
current()函数返回当前被内部指针指向的数组单元的值,并不移动指针。如果内部指针指向超出了单元列表的末端,current()返回FALSE。

警告
如果数组包含有空的单元(0或者"",空字符串)则本函数在碰到这个单元时也返回FALSE。这使得用current()不可能判断是否到了此数组列表的末端。要正确遍历可能含有空单元的数组,用each()函数。

例1.current()及相关函数的用法示例

复制代码代码如下:
<?php
 $transport=array("foot","bike","car","plane");
 $mode=current($transport);//$mode="foot";
 $mode=next($transport);  //$mode="bike";
 $mode=current($transport);//$mode="bike";
 $mode=prev($transport);  //$mode="foot";
 $mode=end($transport);   //$mode="plane";
 $mode=current($transport);//$mode="plane";
 ?>