Home  >  Article  >  Backend Development  >  PHP list() 将数组中的值赋给变量的简单实例_php实例

PHP list() 将数组中的值赋给变量的简单实例_php实例

WBOY
WBOYOriginal
2016-06-16 08:40:55944browse

list()

PHP list() 用一步操作把数组中的值赋给一些变量。同 array() 一样,list() 不是真正的函数,而是语言结构。

语法:

void list( mixed var, mixed ... )注意: list() 仅能用于数字索引的数组并假定数字索引从 0 开始。

例子1:

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

例子2,数据表查询:

$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() 使用数组索引

list() 中允许使用另一个数组来接收数组赋值过来的值,只是当使用索引数组的时候,其赋值顺序跟 list() 中列出的顺序是相反的:

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

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

print_r($a);输出的 $a 数组结构如下:

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

以上这篇PHP list() 将数组中的值赋给变量的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

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