将数组的元素设置为自变量
set array's elements as independent variables
我正在寻找一个 php 函数,它将数组的元素设置为自变量(同名)
我在下面编写了应该可以解决问题的代码...
在 php ?
中是否已经有任何功能可以做到这一点?
$data = array(...);
foreach ($data as $key => $value) {
${$key} = $value;
}
您正在寻找PHP extract():
extract($data);
尝试
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
或者
foreach($ARRAY as $key=>$value) {
$$key = $value;
}
我正在寻找一个 php 函数,它将数组的元素设置为自变量(同名)
我在下面编写了应该可以解决问题的代码... 在 php ?
中是否已经有任何功能可以做到这一点?$data = array(...);
foreach ($data as $key => $value) {
${$key} = $value;
}
您正在寻找PHP extract():
extract($data);
尝试
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
或者
foreach($ARRAY as $key=>$value) {
$$key = $value;
}