Home >Backend Development >PHP Tutorial >How to get the first value of an array in php_PHP Tutorial

How to get the first value of an array in php_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:371194browse

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

Explanation
mixed reset (array &array)
reset() rewinds array's internal pointer to the first element and returns the first array element The value, returns FALSE if the array is empty.

Example 1. reset() example

Copy code The code is as follows:

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

next(PHP 3, PHP 4, PHP 5)
next -- will The internal pointer in the array moves forward one position

Description
mixed next ( array &array )
Returns the value of the next cell pointed to by the array's internal pointer, or 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

Copy code The code is as follows:

$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(PHP 3, PHP 4, PHP 5)
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

Copy code The code is as follows:

$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';
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328073.htmlTechArticlereset (PHP 3, PHP 4, PHP 5) reset -- Point the internal pointer of the array to the first element Description mixed reset (array lt;?php $array = array('stepone', 'step two', 'step three', 'step four...
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