Home >Backend Development >PHP Tutorial >Array assignment PHP list A simple example of assigning values ​​in an array to variables

Array assignment PHP list A simple example of assigning values ​​in an array to variables

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-28 08:29:211101browse

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.

Syntax:

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() using array index

list() allows the use of another array to receive the value assigned by the array, just When using an indexed array, the order of assignment is opposite to 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 values ​​​​in an array to variables is all the content shared by the editor. I hope it can give you a reference, and I hope you will support it. This site.

The above has introduced a simple example of array assignment using PHP list to assign values ​​in an array to variables, including the content of array assignment. I hope it will be helpful to friends who are interested in PHP tutorials.

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