How PHP Gets the first value of an array _php tips

Source: Internet
Author: User
Tags mixed prev

Reset (PHP 3, PHP 4, PHP 5)
Reset--Points the array's internal pointer to the first cell

Description
Mixed reset (array &array)
Reset () returns the array's internal pointer back to the first cell and returns the value of the first array cell, FALSE if the array is empty.

Example 1. Reset () example

Copy Code code as follows:

<?php
$array = Array (' Stepone ', ' Step two ', ' Step three ', ' step Four ');
By default, the pointer was on the
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-Moves the internal pointer in the array forward one

Description
Mixed next (array &array)
Returns the value of the next cell that is pointed to by an array's internal pointer, or FALSE if there are no more cells.

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.

Warning
If the array contains empty cells, or if the cell value 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.

Example 1. Next () and the usage examples of related functions

Copy Code code as follows:

<?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 (PHP 3, PHP 4, PHP 5)
Current--Returns the active cell in the array

Description
Mixed current (array &array)

Each array has an internal pointer to its "current" cell, which initially points to the first cell inserted into the array.
The current () function returns the value of the array cell that is currently pointed to by the internal pointer, and does not move the pointer. 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.

Example 1. Example of the use of current () and related functions

Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.