Php: how to obtain the value of the first array element in an array element

Source: Internet
Author: User
This article mainly introduces how php can obtain the value of the first array element in the array element, and analyzes in detail the usage of reset, next, current and other functions in array operations, it is very practical. if you need it, you can refer to the following example to describe how php can obtain the value of the first array element in an array element. Share it with you for your reference. The details are as follows:

In php built-in functions, the functions used to obtain array element values mainly include the reset next current prev end functions.

Reset (PHP 3, PHP 4, PHP 5)

Function syntax: mixed reset (array & array)

Purpose: This function returns the internal pointer of array to the first unit and the value of the first array unit. If the array is null, FALSE is returned. the code is as follows:

The code is 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 syntax: mixed next (array & array)

Purpose: return the value of the next unit pointed to by the internal pointer of the array, or FALSE if no more units exist. the code is as follows:

The code is 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 the unit value is 0, this function returns FALSE if it encounters these cells. to traverse arrays that may contain empty cells or whose unit value is 0 correctly, see the each () function.

Current (PHP 3, PHP 4, PHP 5)

Function syntax: mixed current (array & array)

Purpose: return the value of the array unit to which the internal pointer points. if the internal pointer points to the first unit inserted into the array, current () returns FALSE.

Warning:If the array contains an empty unit (0 or "", null string), this function returns FALSE when this unit is encountered. This makes it impossible to use current () to determine whether it has reached the end of the array list. To traverse arrays that may contain null cells correctly, use the each () function.

The behavior of next () and current () is similar. there is only one difference. before returning the value, move the internal pointer one byte forward. This means that it returns the value of the next array unit and moves the array pointer one bit forward. If the result of moving the pointer exceeds the end of the array unit, next () returns FALSE.

The following is an example of using related functions. the code is as follows:

The code is 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 PHP programming.

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.