PHP gets the implementation method of the first array element value in the array element _php tips

Source: Internet
Author: User
Tags function definition mixed prev

This article illustrates how PHP gets the first array element value in an array element. Share to everyone for your reference. Specifically as follows:

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, or FALSE if the array is empty, as follows:

Copy Code code as follows:
$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 returns FALSE when there are no more units, as follows:

Copy Code code as follows:
$array =array (' Step One ', ' Step two ', ' www ', ' phpernote.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, does not move the pointer, initially points to the first cell inserted into the array, and if the internal pointer points to the end of the list of cells, current () returns FALSE.

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, as follows:

Copy Code code as follows:
$transport = Array (' foot ', ' www ', ' car ', ' phpernote ', ' 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 ';

I hope this article will help you with your PHP program design.

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.