看PHP手册,有段话不太理解解决思路
程序员文章站
2022-06-16 19:32:18
...
看PHP手册,有段话不太理解
在介绍list时候,给出了如下warning:
list() assigns the values starting with the right-most parameter. If you are using plain variables, you don't have to worry about this. But if you are using arrays with indices you usually expect the order of the indices in the array the same you wrote in the list() from left to right; which it isn't. It's assigned in the reverse order.
我自己翻译不太明白,list有下标吗?list为什么从右到左赋值?什么情况是他所说的list下标和数组下标相反?
我是新手,希望哪位高手予以解答,不胜感激!!!!
------解决方案--------------------
手册下面还有例子呢
$info = array('coffee', 'brown', 'caffeine');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
输出
array(3) {
[2]=>
string(8) "caffeine"
[1]=>
string(5) "brown"
[0]=>
string(6) "coffee"
}
看出问题了没、?如果没,这样子运行
$info = array('coffee', 'brown', 'caffeine');
list($a[], $a[], $a[]) = $info;
var_dump($a);
------解决方案--------------------
示例1
在介绍list时候,给出了如下warning:
list() assigns the values starting with the right-most parameter. If you are using plain variables, you don't have to worry about this. But if you are using arrays with indices you usually expect the order of the indices in the array the same you wrote in the list() from left to right; which it isn't. It's assigned in the reverse order.
我自己翻译不太明白,list有下标吗?list为什么从右到左赋值?什么情况是他所说的list下标和数组下标相反?
我是新手,希望哪位高手予以解答,不胜感激!!!!
------解决方案--------------------
手册下面还有例子呢
$info = array('coffee', 'brown', 'caffeine');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
输出
array(3) {
[2]=>
string(8) "caffeine"
[1]=>
string(5) "brown"
[0]=>
string(6) "coffee"
}
看出问题了没、?如果没,这样子运行
$info = array('coffee', 'brown', 'caffeine');
list($a[], $a[], $a[]) = $info;
var_dump($a);
------解决方案--------------------
示例1
- PHP code
list($a, $b, $c) = array(1, 2, 3);print_r(array($a, $b, $c));
------解决方案--------------------
你说的有中文注释吧!http://php.net/manual/zh/function.list.php
- HTML code
Warninglist() 从最右边一个参数开始赋值。如果你用单纯的变量,不用担心这一点。但是如果你用了具有索引的数组,通常你期望得到的结果和在 list() 中写的一样是从左到右的,但实际上不是。是以相反顺序赋值的。相关文章
相关视频