【问题标题】:Passing variable elements to variables with the elements' names [duplicate]将变量元素传递给具有元素名称的变量[重复]
【发布时间】:2019-03-11 15:04:09
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您正在寻找的功能由extract() PHP 函数提供。
  • 虽然我要补充一点,这是一个坏主意,而且很少真正需要 - 恕我直言。

标签: php


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    相关资源
    最近更新 更多