List () in PHP, each (), reset () function application

Source: Internet
Author: User

1.list function

The list function assigns the values in the array to some variables, with the following syntax:

List Mixed $varname Mixed $varname...)

Like Array (), this is not a real function, but a language structure. list () assigns a set of variables in one-step operation.

Note: list () can only be used for arrays of numeric indexes and assumes that the numeric index starts at 0.

<? PHP $arr=array(+/-); List ($a,$b,$c) =$arr; Echo "\ $a is $a, <br/>\ $b is $b, <br/>\ $c is $c.<br/>";? >

Shown as:

$a is 1,
$b is 2,
$c is 3.

Note: The index of the list function must be a number and must also start at 0.

2.each function and reset function

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

The function syntax:

Array  each array &$array )

Returns the key/value pair of the current pointer position in an array and moves the array pointer forward.

A key-value pair is returned as an array of four cells, with a key value of 0 ,, 1 , key and value . 0the cell and key the key name that contains the array cell, 1 and contains the value data.

If the internal pointer crosses the end of the array, each () returns FALSE. Why does each function have four subscripts?

In fact, each function to get these four subscript is only convenient for us to operate, we can use 0,1 as an index, you can also use Key,value as an index.

Code:

<?PHP$arr=Array("I am the first value", "I am the second value", "I am the third value");Echo"When we use 0,1 as index:<br/><br/>";$a= each($arr);Echo"My position in the \ $arr array is:".$a[' 0 '];Echo"<br/>";Echo"I have a value in the \ $arr array:".$a[' 1 '];Echo"<br/><br/>";Echo"When we use Key,value as index:<br/><br/>";$b= each($arr);Echo"My position in the \ $arr array is:".$b[' Key '];Echo"<br/>";Echo"I have a value in the \ $arr array:".$b[' Value '];?>

Shown as:

When we use 0,1 as an index:
My position in the $arr array is: 0
My value in the $arr array is: I am the first value
When we use Key,value as an index:
My position in the $arr array is: 1
My value in the $arr array is: I'm the second value.

You can also use the each function to combine with the list function to iterate through an array , as in the following example:

<? PHP $fruit Array (' a ' + = ' apple ', ' b ' = ' banana ', ' c ' = ' cranberry '); Reset ($fruit);  while (list($key$valeach ($fruit)) {      Echo "$key$val\ n";}? >

Shown as:

A = apple b = Banana c = Cranberry

Note that the index of the list function must be a number and must also start at 0.

The reset function inside explains:

After each () , the array pointer stays in the next cell in the array, or when the end of the array is encountered, the last cell. If you want to iterate through the array again, you must use Reset (). if it is the first time, the group can not use.

Say a bit more about the Reset function:

Reset is to point the inner pointer of the array to the first cell, with the syntax:

Mixed Reset array &$array )

Reset () returns the internal pointer of the array back to the first cell and returns the value of the first array cell, FALSEIf the array is empty.

The above example 2 can be compared with the following example, it is easy to understand ...

Code:

<?PHP$arr=Array("I am the first value", "I am the second value", "I am the third value");Echo"When we use 0,1 as index:<br/><br/>";$a= each($arr);Echo"My position in the \ $arr array is:".$a[' 0 '];Echo"<br/>";Echo"I have a value in the \ $arr array:".$a[' 1 '];Echo"<br/><br/>";Echo"When we use Key,value as index:<br/><br/>";Reset($arr);$b= each($arr);Echo"My position in the \ $arr array is:".$b[' Key '];Echo"<br/>";Echo"I have a value in the \ $arr array:".$b[' Value '];?>

Shown as:

When we use 0,1 as an index:

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

When we use Key,value as an index:

My position in the $arr array is: 0
My value in the $arr array is: I am the first value //Note the comparison with this line in Example 2.

List () in PHP, each (), reset () function application

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.