List () in PHP, each (), reset () function using reprint

Source: Internet
Author: User
List () in PHP, each (), reset () function application reprint

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:

$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 . 0the cell and key the key name that contains the array cell, 1 and contains the value 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:

$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 "I am in \ $arr The value in the array is:". $a [' 1 '];
echo "

";
echo "When we use Key,value as index:

";
$b =each ($arr);
echo "My position in \ $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:

$fruit = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' cranberry ');
Reset ($fruit);
while (list ($key, $val) = each ($fruit)) {
????? echo "$key = $val \ n";
}
?>
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, FALSEIf the array is empty.

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

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 \ $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
My value in the $arr array is: I am the first value???????? //Note this line in Example 2 shows that it is clear.

?

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