downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

natcasesort> <ksort
[edit] Last updated: Fri, 17 May 2013

view this page in

list

(PHP 4, PHP 5)

list 把数组中的值赋给一些变量

说明

array list ( mixed $varname [, mixed $... ] )

array() 一样,这不是真正的函数,而是语言结构。 list() 用一步操作给一组变量进行赋值。

参数

varname

一个变量。

返回值

返回指定的数组。

范例

Example #1 list() 例子

<?php

$info 
= array('coffee''brown''caffeine');

// 列出所有变量
list($drink$color$power) = $info;
echo 
"$drink is $color and $power makes it special.\n";

// 列出他们的其中一个
list($drink, , $power) = $info;
echo 
"$drink has $power.\n";

// 或者让我们跳到仅第三个
list( , , $power) = $info;
echo 
"I need $power!\n";

// list() 不能对字符串起作用
list($bar) = "abcde";
var_dump($bar); // NULL
?>

Example #2 list() 用法的一个例子

<table>
 <tr>
  <th>Employee name</th>
  <th>Salary</th>
 </tr>

<?php

$result 
mysql_query("SELECT id, name, salary FROM employees"$conn);
while (list(
$id$name$salary) = mysql_fetch_row($result)) {
    echo 
" <tr>\n" .
          
"  <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
          
"  <td>$salary</td>\n" .
          
" </tr>\n";
}

?>

</table>

Example #3 使用嵌套的 list()

<?php

list($a, list($b$c)) = array(1, array(23));

var_dump($a$b$c);

?>
int(1)
int(2)
int(3)

Example #4 在 list() 中使用数组索引

<?php

$info 
= array('coffee''brown''caffeine');

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

var_dump($a);

?>

产生如下输出(注意单元顺序和 list() 语法中所写的顺序的比较):

array(3) {
  [2]=>
  string(8) "caffeine"
  [1]=>
  string(5) "brown"
  [0]=>
  string(6) "coffee"
}

注释

Warning

list() 从最右边一个参数开始赋值。如果你用单纯的变量,不用担心这一点。 但是如果你用了具有索引的数组,通常你期望得到的结果和在 list() 中写的一样是从左到右的,但实际上不是。 它是以相反顺序赋值的。

Warning

list() 执行过程中修改数组(比如使用 list($a, $b) = $b)将会产生不可预知的结果。

Note:

list() 仅能用于数字索引的数组并假定数字索引从 0 开始。

参见

  • each() - 返回数组中当前的键/值对并将数组指针向前移动一步
  • array() - 新建一个数组
  • extract() - 从数组中将变量导入到当前的符号表



natcasesort> <ksort
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes list - [3 notes]
up
15
chris at chlab dot ch
6 months ago
The example states the following:
<?php
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar);
// output: NULL
?>

If the string is in a variable however, it seems using list() will treat the string as an array:
<?php
$string
= "abcde";
list(
$foo) = $string;
var_dump($foo);
// output: string(1) "a"
?>
up
9
svennd
4 months ago
The list() definition won't throw an error if your array is longer then defined list.
<?php

list($a, $b, $c) = array("a", "b", "c", "d");

var_dump($a); // a
var_dump($b); // b
var_dump($c); // c
?>
up
0
srikanth at networthindia dot com
3 months ago
Note: list cannot assign array cast of object to variables straight away. first you need to convert the object to numeric indexed array.

ex:
list($a, $b, $d) = (array) $abc; // $abc is an object; this will not assign.
list($a, $b, $c) = array_values((array) $abc); // This will work.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites