PHP list() 将数组中的值赋给变量的简单实例,list数组_PHP教程

WBOY
Release: 2016-07-12 08:49:39
Original
832 people have browsed it

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

list()

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

语法:

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

例子1:

<?php
$arr_age = array(18, 20, 25);
list($wang, $li, $zhang) = $arr_age;
echo $wang;    //输出:18
echo $zhang;    //输出:25
?>
Copy after login

例子2,数据表查询:

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

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() 将数组中的值赋给变量的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1136640.htmlTechArticlePHP list() 将数组中的值赋给变量的简单实例,list数组 list() PHP list() 用一步操作把数组中的值赋给一些变量。同 array() 一样,list() 不是真正...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!