首页 > 解决方案 > 将变量元素传递给具有元素名称的变量

问题描述

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

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

我想要一个快捷方式来输入这个:

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

标签: php

解决方案


当然你可以使用$$

$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;

推荐阅读