简体   繁体   English

是否有一种简便的方法将一组变量分配给php中数组的每个值?

[英]Is there a shorthand way to assign a group of variables to each of the values of an array in php?

In perl I can write 在Perl中,我可以写

($var1, $var2, $var3) = split(/:/, "foo:bar:blarg")

to split a string by colon and assign each array element to $var1, $var2 and $var3. 用冒号分割字符串,并将每个数组元素分配给$ var1,$ var2和$ var3。 Is there a similar syntax for this in php? 在php中有类似的语法吗? I have to use the variables in a lot of my code so I want them to have meaningful names, and writing variable assignment code like 我必须在很多代码中使用变量,因此我希望它们具有有意义的名称,并编写变量赋值代码,例如

$array = split(/:/, "foo:bar:blarg");
$var1 = $array[0];
$var2 = $array[1];
//etc 

is tedious when i have to do this a lot. 当我不得不做很多事情时,这是很乏味的。

PHP具有list语言构造来做到这一点。

list($var1, $var2, $var3) = split(":", "foo:bar:blarg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM