【发布时间】:2023-03-04 19:49:01
【问题描述】:
我不确定我的记忆是不是错了,但是当我最后一次使用 PHP 时(几年前),我隐约记得做过这样的事情:
$firstVariable, $secondVariable = explode(' ', 'Foo Bar');
请注意,上面的语法不正确,但在本例中,它会将 'Foo' 分配给 $firstVariable,并将 'Bar' 分配给 $secondVariable。
正确的语法是什么?
谢谢。
【问题讨论】:
我不确定我的记忆是不是错了,但是当我最后一次使用 PHP 时(几年前),我隐约记得做过这样的事情:
$firstVariable, $secondVariable = explode(' ', 'Foo Bar');
请注意,上面的语法不正确,但在本例中,它会将 'Foo' 分配给 $firstVariable,并将 'Bar' 分配给 $secondVariable。
正确的语法是什么?
谢谢。
【问题讨论】:
list($firstVar, $secondVar) = explode(' ', 'Foo Bar');
list() 是你所追求的。
【讨论】:
首先是几个单独使用 list() 的示例,然后是 2 个使用 list() 结合 explode() 的示例。
PHP manual page for list() 上的例子特别有启发性:
基本上,您的列表可以随心所欲,但它是一个绝对列表。换句话说,数组中项目的顺序显然很重要,要跳过某些内容,您必须在 list() 中将相应的位置留空。
最后,你不能列出字符串。
<?php
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>
举几个例子:
将list()、explode() 和Arrays 应用于功能失调的关系:
<?php
// What they say.
list($firstVar, $secondVar, , , $thirdVar) = explode(' ', 'I love to hate you');
// What you hear.
// Disaplays: I love you
echo "$firstVar $secondVar $thirdVar";
?>
最后,您可以将 list() 与数组结合使用。 $VARIABLE[] 将一个项目存储到数组的最后一个槽中。应该注意存储的顺序,因为它可能与您期望的相反:
<?php
list(, $Var[], ,$Var[] , $Var[]) = explode(' ', 'I love to hate you');
// Displays:
// Array ( [0] => you [1] => hate [2] => love )
print_r($Var);
?>
list() 手册页上的警告中给出了为什么存储东西的顺序的解释:
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.
【讨论】:
从php7.1开始,你可以做对称数组解构。
代码:(Demo)
$array = [1, 2, 3];
[$a, $b, $c] = $array;
echo "$a $b $c";
// displays: 1 2 3
无需调用list()。
如需深入分析和示例,请仔细查看此帖子:https://sebastiandedeyne.com/the-list-function-and-practical-uses-of-array-destructuring-in-php/
至于explode()配合list()使用或者数组解构,如果不能保证一定数量的元素,
最佳实践是声明explode() 的第三个参数以限制生成元素的数量。这不会强制生产这么多元素;相反,它只会告诉 php 在达到该元素数量时停止爆炸。
[$firstVariable, $secondVariable] = explode(' ', $stringToBeHalved, 2);
如果您不能 100% 保证您的分解数据将为赋值运算符的另一端提供平衡数据,您可以使用上述技术实现最大值并使用类似于 array_replace() 的方法来提供最小值赋值运算符右侧的元素。
代码:(Demo)
$strings = [
"1,2,3,4",
"1,2,3",
"1,2"
];
$minElements = 3;
$defElements = array_fill(0, $minElements, null);
foreach ($strings as $string) {
[$a, $b, $c] = array_replace($defElements, explode(',', $string, $minElements));
var_export($a);
echo ' _ ';
var_export($b);
echo ' _ ';
var_export($c);
echo "\n-----------------\n";
}
输出:
'1' _ '2' _ '3,4'
-----------------
'1' _ '2' _ '3'
-----------------
'1' _ '2' _ NULL
-----------------
另外一种很少被推荐的技巧是致电extract()。如果您无法完全控制它正在处理的数据,此函数可能会很危险,因为它会将所有数据的键作为变量名推入全局范围 - 可能会覆盖变量并导致脚本漏洞或损坏。
要准备提取数据,通过分配键将索引数组转换为关联数组。
代码:(Demo)
$threeKeys = ['a', 'b', 'c'];
foreach ($strings as $string) {
extract(array_combine($threeKeys, explode(',', $string, 3)));
// now you can use $a, $b, and $c
}
上述演示显示了在未提供平衡/预期数据量时生成的一些警告。所以这个故事的寓意是要小心并通过确保数据处理器始终收到他们需要的东西来消除可能的边缘情况。
【讨论】: