Php method for obtaining the value of the first array element in an array element

Source: Internet
Author: User
Tags arrays function definition mixed

Reset (PHP 3, PHP 4, PHP 5)

Function definition mixed reset (array & array)

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. For example:

 

The code is as follows: Copy code

<? 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)

Purpose: return the value of the next unit pointed to by the internal pointer of the array, or FALSE if no more units exist. For example:

The code is as follows: Copy code

<? Php
$ Array = array ('step one', 'step two', 'WWW ', 'phpernote. Com', 'step four ');
Echo next ($ array );


// Output: step two
Warning if the array contains an empty unit or the unit value is 0, FALSE is returned when this function encounters these units. To correctly traverse arrays that may contain null cells or whose unit value is 0, see the each () function.

Current (PHP 3, PHP 4, PHP 5)

Function definition mixed current (array & array)

Purpose: return the value of the array unit pointed to by the internal pointer, without moving the pointer. Initially point to the first unit inserted into the array. 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.

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: Copy code

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

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.