Home >Backend Development >PHP Tutorial >PHP method to get the first value of an array - PHP tutorial

PHP method to get the first value of an array - PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:15:021400browse

reset (PHP 3, PHP 4, PHP 5)
reset -- Point the internal pointer of the array to the first element

Description
mixed reset (array &array)
reset() rewinds the internal pointer of array to the first cell and returns the value of the first array cell, or FALSE if the array is empty.

Example 1. reset() example

<?<span php
</span><span //</span><span 获取数组第一个值
//by www.jbxue.com</span>
<span $array</span> = <span array</span>('stepone', 'step two', 'step three', 'step four'<span );
</span><span //</span><span by default, the pointer is on the first element</span>
<span echo</span> <span current</span>(<span $array</span>) . "<br/>\n"; <span //</span><span  "stepone"
// skip twosteps</span>
<span next</span>(<span $array</span><span );
</span><span next</span>(<span $array</span><span );
</span><span echo</span> <span current</span>(<span $array</span>) . "<br/>\n"; <span //</span><span  "stepthree"
//reset pointer, start again on step one</span>
<span reset</span>(<span $array</span><span );
</span><span echo</span> <span current</span>(<span $array</span>) . "<br/>\n"; <span //</span><span  "stepone"</span>
?>

next -- Move the internal pointer in the array forward one position

Description
mixed next (array &array)
Returns the value of the next cell pointed to by the internal pointer of the array, or returns FALSE when there are no more cells.

next() behaves similarly to current(), with one difference: the internal pointer is moved forward one position before returning the value. This means that it returns the value of the next array element and moves the array pointer forward one position. If moving the pointer results in moving the pointer beyond the end of the array element, next() returns FALSE.

Warning
If the array contains empty cells, or the value of the cell is 0, this function will also return FALSE when encountering these cells. To correctly iterate over an array that may contain empty cells or a cell value of 0, see the each() function.

Example 1. Usage examples of next() and related functions

<?<span php
</span><span //</span><span 数组迭代方法 next
//by www.jbxue.com</span>
<span $transport</span> = <span array</span>('foot', 'bike', 'car', 'plane'<span );
</span><span $mode</span> = <span current</span>(<span $transport</span>); <span //</span><span $mode = 'foot';</span>
<span $mode</span> = <span next</span>(<span $transport</span>); <span //</span><span  $mode ='bike';</span>
<span $mode</span> = <span next</span>(<span $transport</span>); <span //</span><span  $mode ='car';</span>
<span $mode</span> = <span prev</span>(<span $transport</span>); <span //</span><span  $mode ='bike';</span>
<span $mode</span> = <span end</span>(<span $transport</span>); <span //</span><span  $mode ='plane';</span>
?>

current -- Returns the current cell in the array

Description
mixed current (array &array)

Each array has an internal pointer pointing to its "current" cell, initially pointing to the first cell inserted into the array.
The current() function returns the value of the array unit currently pointed to by the internal pointer, without moving the pointer. If the internal pointer points beyond the end of the unit list, current() returns FALSE.

Warning
If the array contains an empty cell (0 or "", empty string), this function will also return FALSE when encountering this cell. This makes it impossible to use current() to determine whether the end of the array list has been reached. To correctly iterate over an array that may contain empty cells, use the each() function.

Example 1. Usage examples of current() and related functions

<?<span php
</span><span //</span><span 取当前数组元素</span>
<span $transport</span> = <span array</span>('foot', 'bike', 'car', 'plane'<span );
</span><span $mode</span> = <span current</span>(<span $transport</span>); <span //</span><span $mode = 'foot';</span>
<span $mode</span> = <span next</span>(<span $transport</span>); <span //</span><span  $mode ='bike';</span>
<span $mode</span> = <span current</span>(<span $transport</span>); <span //</span><span $mode = 'bike';</span>
<span $mode</span> = <span prev</span>(<span $transport</span>); <span //</span><span  $mode ='foot';</span>
<span $mode</span> = <span end</span>(<span $transport</span>); <span //</span><span  $mode ='plane';</span>
<span $mode</span> = <span current</span>(<span $transport</span>); <span //</span><span $mode = 'plane';
//by www.jbxue.com 脚本学堂</span>
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440221.htmlTechArticlereset (PHP 3, PHP 4, PHP 5) reset -- Point the internal pointer of the array to the first element Description mixed reset (array array) reset() rewinds the array's internal pointer to the first element and returns...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn