This article mainly describes how to get the value of the first array cell in PHP in a method. In PHP's built-in functions, the function that gets the value of an array element is mainly the reset next current prev.
Reset (PHP 3, PHP 4, PHP 5)
function definition mixed reset (array &array)
Action: The function returns the internal pointer of an array to the first cell and returns the value of the first array cell, FALSE if the array is empty. For example:
<?php
$array =array (' Step One ', ' Step two ', ' Step three ', ' step Four ');
echo Reset ($array);
Output: Step One
Next (PHP 3, PHP 4, PHP 5)
function definition mixed next (array &array)
Function: Returns the value of the next cell that the array's internal pointer points to, or FALSE if there are no more cells. For example:
<?php
$array =array (' Step One ', ' Step two ', ' www ', ' alixixi.com ', ' step Four ');
Echo Next ($array);
Output: Step Two
warning : If the array contains empty cells, or if the value of the cell is 0, the function encounters these cells and returns FALSE. To properly traverse an array that may contain empty cells or a cell value of 0, see the each () function.
Current (PHP 3, PHP 4, PHP 5)
function definition mixed current (array &array)
Function: Returns the value of the array cell currently pointed to by the internal pointer, without moving the pointer. Initially points to the first cell inserted into the array. Current () returns FALSE if the internal pointer points beyond the end of the list of cells.
warning : If the array contains an empty cell (0 or "", an empty string) The function also returns FALSE when it encounters this cell. This makes it impossible to determine whether or not the end of this array list is possible with current (). To properly traverse an array that may contain empty cells, use the each () function.
The behavior of next () and current () is similar, with only one point of difference, in which the internal pointer is moved forward one bit before the value is returned. This means that it returns the value of the next array cell and moves the array pointer forward one bit. If the result of moving the pointer is beyond the end of the array cell, next () returns FALSE.
The following is an example of the use of related functions:
<?php
$transport = array (' foot ', ' www ', ' car ', ' alixixi ', ' com ');
$mode = current ($transport); $mode = ' foot ';
$mode = Next ($transport); $mode = ' www ';
$mode = Next ($transport); $mode = ' car ';
$mode = prev ($transport); $mode = ' www ';
$mode = End ($transport); $mode = ' com ';
$mode = current ($transport); $mode = ' com ';
$mode = Reset ($transport); $mode = ' foot ';
Articles that you may be interested in
- PHP uses Array_flip to implement array key exchange to remove duplicate value of array
- JavaScript Array details (how the array is declared and how it is calculated)
- A summary of the methods of JS array to turn string and JS string to array
- A summary of the methods for removing duplicate values from two-dimensional PHP arrays
- How PHP deletes the first and last elements of an array
- PHP generates a continuous number (letter) Array function range () analysis, PHP lottery program function
- Introduction to the parallel set, intersection and difference set functions of arrays in PHP
- PHP gets the last element of the array