background image

php 如何获取数组第一个值

reset (PHP 3, PHP 4, PHP 5)
reset -- 将数组的内部指针指向第一个单元
说明
mixed reset ( array &array )
reset() 将 array 的内部指针倒回到第一个单元并返回第一个数组单元的值,如果数组为空则
返回

 FALSE。

 1. reset() 例子

代码如下

:

<?php
 $array = array('stepone', 'step two', 'step three', 'step four');
 //by default, the pointer is on the first element
 echo current($array) . "<br/>\n"; // "stepone"
 // skip twosteps
 next($array);
 next($array);
 echo current($array) . "<br/>\n"; // "stepthree"
 //reset pointer, start again on step one
 reset($array);
 echo current($array) . "<br/>\n"; // "stepone"
 ?>

next(PHP 3, PHP 4, PHP 5)
next --  

 

将数组中的内部指针向前移动一位

说明
mixed next ( 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';