PHP list (), each (), reset () function Application _php Tutorial

Source: Internet
Author: User
This article introduces the PHP list (), each (), reset () function application in detail, there is a need to know friends can refer to.

1.list function

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

void 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.

Example 1. List () example

Code:

The code is as follows Copy Code

$arr =array (a);
List ($a, $b, $c) = $arr;
echo "$a is $ A,
$b is $b,
$c is $c.
";
?>

Shown as:

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

Note that 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, and the key

Values are 0,1,key and value. Unit 0 and key contain the key name of the array cell, and 1 and value contain the data. If the internal

The pointer crosses the end of the array, and each () returns FALSE. Why does each function have four of the following tables? In fact, each letter

The number of these four subscripts is only convenient for us to operate, we can use 0,1 as an index, or we can use Key,value as an index.

Example 2:each Example

Code:

The code is as follows Copy 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 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 we use Key,value as index:

";
$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 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:

Example 3:

The code is as follows Copy Code
$fruit = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' cranberry ');
Reset ($fruit);
while (list ($key, $val) = each ($fruit)) {
echo "$key = $valn";
}
?>

Shown as:

A = apple b = Banana c = Cranberry

Note here 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, FALSE if the array is empty.

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

Code:

The code is as follows Copy 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 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 we use Key,value as index:

";
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 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
I am in the $arr array The value is: I am the first value//Note In Example 2, this line shows that you understand

http://www.bkjia.com/PHPjc/631626.html www.bkjia.com true http://www.bkjia.com/PHPjc/631626.html techarticle This article introduces the PHP list (), each (), reset () function application in detail, there is a need to know friends can refer to. The 1.list function list function assigns the values in the array to some variables, the syntax is as follows ...

  • Related Article

    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.