The list (), each (), and reset () functions in PHP are reprinted.

Source: Internet
Author: User
The list (), each (), and reset () function applications in PHP are reproduced. the list function of the list function assigns values in the array to some variables. The syntax is as follows: voidlist (mixed $ varname, mixed $ varname ...) such as list (), each (), reset () function application in ar PHP

1. list function

The list function assigns values in the array to some variables. The syntax is as follows:

VoidList(Mixed $ varname, mixed $ varname ...)

Like array (), this is not a real function, but a language structure.List ()Assign values to a group of variables in one step.

???????????Note: list () can only be used as an array of numeric indexes and assumes that the numeric index starts from 0.

Example 1.List ()Example

Code:

$ Arr = array (1, 2, 3 );
List ($ a, $ B, $ c) = $ arr;
Echo "\ $ a is $,
\ $ B is $ B,
\ $ C is $ c.
";
?>

Shown:

$ A is 1,
$ B is 2,
$ C is 3 .????????????Note that the subscript of the list function must be a number and start from 0.

2. each and reset functions

The each function returns the current key/value pair in the array and moves the array pointer one step forward. Note that it is a pair, which is described in detail below.

Syntax of this function:

ArrayEach(Array & $ array)

ReturnArrayThe key/value pair of the current pointer position in the array and move the array pointer forward. The key-value pair is returned as an array of four units.

The value is0,1,KeyAndValue. Unit0AndKeyKey name that contains an array unit,1AndValueContains data. If

If the pointer goes beyond the end of the arrayEach ()ReturnFALSE. Why are there four tables in the each function? In fact, the each letter

We can use 0, 1 as the index, or key and value as the index.

Example 2: each

Code:

$ Arr = array ("I am the first value", "I am the second value", "I am the third value ");
Echo "when we use 0, 1 as the index:

";
$ A = each ($ arr );
Echo "my position in the \ $ arr array is:". $ a ['0'];
Echo"
";
Echo "my value in the \ $ arr array is:". $ a ['1'];
Echo"

";
Echo "when key and value are used as indexes:

";
$ B = each ($ arr );
Echo "my position in the \ $ arr array is:". $ B ['key'];
Echo"
";
Echo "???? My value in the \ $ arr array is: ". $ B ['value'];
?>

Shown:

When we use 0, 1 as the index:
My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value.
When key and value are used as indexes:
My position in the $ arr array is: 1
My value in the $ arr array is: I am the second value.

You can also combine the each function with the list function.Traverse arrays, For example:

Example 3:

$ Fruit = array ('a' => 'apple', 'B' => 'bana', 'C' => 'Cranberry ');
Reset ($ fruit );
While (list ($ key, $ val) = each ($ fruit )){
????? Echo "$ key => $ val \ n ";
}
?>
Shown:

A => apple B => banana c => cranberry

Here you wantNote that the subscript of the list function must be a number and start from 0.

Description of the reset function:

In executionEach ()Then, the array pointer will stay in the next unit of the array or stay in the last unit when it comes to the end of the array. If you want to use each to traverse the array, you must use reset ().If this is the first time you traverse the array, you do not need to use it.

For details, refer to the reset function:

Reset is to point the internal pointer of the array to the first unit. syntax:

MixedReset(Array & $ array)

Reset () willArrayReturns the value of the first array unit. If the array is emptyFALSE.

You can compare the above example 2 with the following example to understand it easily... ,

Code:

$ Arr = array ("I am the first value", "I am the second value", "I am the third value ");
Echo "when we use 0, 1 as the index:

";
$ A = each ($ arr );
Echo "my position in the \ $ arr array is:". $ a ['0'];
Echo"
";
Echo "???? My value in the \ $ arr array is: ". $ a ['1'];
Echo"

";
Echo "when key and value are used as indexes:

";
Reset ($ arr );
$ B = each ($ arr );
Echo "my position in the \ $ arr array is:". $ B ['key'];
Echo"
";
Echo "???? My value in the \ $ arr array is: ". $ B ['value'];
?>

Shown:

When we use 0, 1 as the index:

My position in the $ arr array is: 0
My value in the $ arr array is: I am the first value.

When key and value are used as indexes:

My position in the $ arr array is: 0
My value in the $ arr array is: I am the firstI????????// Note that this line in Example 2 is displayed.

?

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.