繁体   English   中英

PHP内置函数将数组的每个元素分配给变量,它是否存在?

[英]PHP built-in function to assign each element of an array to a variable, does it exist?

例如:

$array = array('f', 'b');
assign($foo, $bar, $array);

// now $foo = 'f' and $bar = 'b'

PHP语言中是否存在类似的内容? 我以前从来不需要这样的东西,也找不到能做到这一点的东西。

我只是想确保自己写函数之前-我不想写语言中已经存在的东西。

list ($foo, $bar) = $array;

list()array()相反,它是一种语言构造。 尤其重要的是要知道,即使在手册的函数参考( list() )中list()它, 也不是,因为没有函数是可写的。

非常接近PHP的extract()函数。

但是,您需要指定var名称,因为它们是数组中每个值的键。

$array = array('foo' => 'f', 'bar' => 'b');
extract($array);
// now $foo = 'f' and $bar = 'b'

您可以使用php的list()函数

$array = array('f', 'b');
list($foo, $bar) = $array;

现在它是

$foo = 'f' and $bar = 'b';

php.net/list

暂无
暂无

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

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