(PHP 4, PHP 5, PHP 7)
list — 把数组中的值赋给一组变量
array list ( mixed
$var1
[, mixed $...
] )像 array() 一样,这不是真正的函数,而是语言结构。 list() 可以在单次操作内就为一组变量赋值。
Note:
list() 仅能用于数字索引的数组,并假定数字索引从 0 开始。
Warning
PHP 5 里,list() 从最右边的参数开始赋值; PHP 7 里,list() 从最左边的参数开始赋值。
如果你用单纯的变量,不用担心这一点。 但是如果你用了具有索引的数组,通常你期望得到的结果和在 list() 中写的一样是从左到右的,但在 PHP 5 里实际上不是, 它是以相反顺序赋值的。
- 等号左边是list()函数,右边是索引数组(下标连续的,按0开始);
- 数组中有几个元素,则list()有几个参数(参数必须是变量不能是值,而且变量是自定义的变量),跟数组的值是一一对应关系;
- 在list()函数的参数中,空项也代表元素
<?php list(,,$c) = ['aa','bb','cc']; echo $c;//cc echo '<br>'; list($a1,$b1,$c1) = ['aa1','bb1','cc1']; echo $a1.$b1.$c1;//aa1bb1cc1 echo '<br>'; list($a2,,$c2) = ['aa2','bb2','cc2']; echo $a2.$c2;//aa2cc2 echo '<br>'; list($a3,$b3,$c3) = [1=>'aa3',2=>'bb3',0=>'cc3']; echo $a3.$b3.$c3;//cc3aa3bb3 echo '<br>'; list($a4,$b4,$c4) = [1=>'aa4',2=>'bb4',3=>'cc4']; echo $a4.$b4.$c4;//aa4bb4 Notice: Undefined offset: 0 in D:phpStudyWWWdemofunction.php on line 14 echo '<br>'; list($a5,$b5,$c5) = [1=>'aa5',2=>'bb5','a'=>'cc5']; echo $a5.$b5.$c5;//aa5bb5 Notice: Undefined offset: 0 in D:phpStudyWWWdemofunction.php on line 18 echo '<br>';