将变量元素传递给具有元素名称的变量

时间:2018-10-06 07:51:24

标签: php

是否可以将所有数组元素传递给具有元素名称的变量? 例如,如果我有以下数组:

$test['name']
$test['phone']
$test['address']
etc..

我想输入一个快捷方式:

$name = $test['name'];
$phone = $test['phone'];
$address = $test['address'];
etc..

1 个答案:

答案 0 :(得分:1)

确定您可以使用$$

$test['name'];
$test['phone'];
$test['address'];


$test['name'] = "John";
$test['phone'] = "987987987";
$test['address'] = "Asheville";

foreach($test as $key=>$val){
  $$key = $test[$key];
}
echo $phone;