PHP uses the list function each function to traverse an array (implement foreach) parsing

Source: Internet
Author: User

PHP uses the list function each function to traverse the array (to implement foreach) analysis

Go straight to the subject.

First look at the list function :

list () function is used to assign a value to a set of variables in one operation, and an infinite argument (the argument exceeds the number of elements in an indexed array will be an error), as the following example

$array =array (1,2,3);
List ($a) = $array;//The element "1" in the $array array will be assigned to $a a variable
var_dump ($a);
List ($a, $b) = $array//is about to $array the element "1", "2" one by one in the array to $a, $b two variables
var_dump ($a, $b);
List ($a, $b, $c) = $array//The element one by one in the $array array is assigned to $a, $b, $c three variables var_dump ($a, $b, $c
);
List ($a, $b, $c, $d) = $array;//The argument exceeds the array element, and the error
var_dump ($a, $b, $c, $d);


Note: the list () function can only manipulate or say an array using a numeric index, and assuming that the numeric index starts at 0, the associative array is not assignable, or if it has both a numeric index and an associated index, only the numeric index elements in the array are taken, as the following example.

$sweet  = Array ( 
' a '  =>  ' apple ', '
B '  =>  ' banana ' 
);
List ($x, $y) = $sweet//error, can not assign value to $x, and $y, will directly error, I will not on the map

Example two:

$sweet  = Array ( 
' a '  =>  ' apple ', '
B '  =>  ' banana ',
0	=>	"one",
1	=>	"two"
);
List ($x, $y) = $sweet;//correct, directly using the elements in the array that meet the requirements of the assignment
var_dump ($x, $y);

In general, the list function assigns the elements of an array index in an array to the parameter, and the index value starts with 0.


Next, say the each () function

returns the current key/value pair in the array and moves the array pointer one step forward

after each (), the array pointer stays in the next cell in the array or when the end of the array is encountered. Here's the code:

$sweet  = Array ( 
' a '  =>  ' apple ', '
B '  =>  ' banana ',
0	=>	"One",
1	=>	"two"
);
$result =each ($sweet);
Var_dump ($restult);


The careful person will find (I am the careless), the key-value pair is returned as an array of four cells, with the key named 0,1,key and value. The unit 0 and key contain the key names of the array cells, and 1 and value contain the data.

If you do it again. The array pointer points to the next element

$sweet  = Array ( 
' a '  =>  ' apple ', '
B '  =>  ' banana ',
0	=>	"one",
1	=>	"two"
);
$result =each ($sweet);
Var_dump ($result);
$result 1=each ($sweet);
Var_dump ($result 1);


In fact, the nth time to use each () function returns the key-value pairs of the nth element of the array (which is returned as an array of four cells), and each () returns FALSE if the internal pointer crosses the end of the array.


this time, plus while, you can implement the Foreach function.

$sweet  = Array ( 
' a '  =>  ' apple ', '
B '  =>  ' banana ',
0	=>	"One",
1	=>	"two"
);
while ($x, $y) =each ($sweet)) {
	echo "key: $x => key value: $y";
	echo "<br/>";
}


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.