Home  >  Article  >  php教程  >  PHP list() A simple instance of assigning the values ​​in the array to a variable, list array

PHP list() A simple instance of assigning the values ​​in the array to a variable, list array

WBOY
WBOYOriginal
2016-07-06 14:24:481126browse

PHP list() A simple instance of assigning the values ​​in the array to a variable, list array

list()

PHP list() assigns the values ​​in the array to some variables in one step. Like array(), list() is not a true function, but a language construct.

Grammar:

void list( mixed var, mixed ... )Note: list() can only be used on numerically indexed arrays and assumes that numerical indexing starts from 0.

Example 1:

<&#63;php
$arr_age = array(18, 20, 25);
list($wang, $li, $zhang) = $arr_age;
echo $wang;    //输出:18
echo $zhang;    //输出:25
&#63;>

Example 2, data table query:

$result = mysql_query("SELECT id, username, email FROM user",$conn);
while(list($id, $username, $email) = mysql_fetch_row($result)) {
  echo "用户名:$username<br />";
  echo "电子邮箱:$email";
}

list() uses array index

List() allows the use of another array to receive the values ​​assigned to the array, but when using an index array, the order of assignment is reversed from the order listed in list():

$arr_age = array(18, 20, 25);

list($a[0], $a[1], $a[2]) = $arr_age;

print_r($a); The output $a array structure is as follows:

Array ( [2] => 25 [1] => 20 [0] => 18 )

The above simple example of PHP list() assigning the value in the array to a variable is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia more.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn