How does php obtain the first value of an array?

Source: Internet
Author: User
The following describes how to use php to obtain the first value of an array. if you need it, refer to reset (PHP 3, PHP 4, PHP 5)
Reset -- point the internal pointer of the array to the first unit

Description
Mixed reset (array & array)
Reset () 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.

Example 1. reset ()

The code is as follows:


$ 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 -- move the internal pointer in the array to a forward position

Description
Mixed next (array & array)
Returns the value of the next unit pointed to by the internal pointer of the array, or FALSE if no more units exist.

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.

Warning
If the array contains empty cells or the unit value is 0, FALSE is returned when this function encounters these cells. To correctly traverse arrays that may contain null cells or whose unit value is 0, see the each () function.

Example 1. use next () and related functions

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 unit in the array.

Description
Mixed current (array & array)

Each array has an internal pointer pointing to its "current" unit, initially pointing to the first unit inserted into the array.
The current () function returns the value of the array unit pointed to by the internal pointer, without moving the pointer. If the internal pointer points to an end that exceeds the cell List, 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.

Example 1. use current () and related functions

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

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.