配置 class - 从函数字符串参数获取配置数组

Configuration class - Get configuration array from the function string argument

我有这样的功能:

$conf = array ('test_value' => 1, 'test_value2' => 2);
function GetValueArray($array, $value)
{
     return $array[$value];
}

我正在使用此函数从数组中接收值。我的问题是我不能像这样使用这个功能:

GetValueArray('conf', 'test_value');

如何将 'conf' 转换为名为 conf 的真实数组以接收我的 'test_value'?

因为函数有自己的作用域,所以一定要'globalize'你正在研究的变量。

但是正如 Rizier123 所说,您可以在变量周围使用方括号来动态 get/set 变量。

<?php

$conf = array ('test_value' => 1, 'test_value2' => 2);

function GetValueArray($array, $value)
{
  global ${$array};
  return ${$array}[$value];
}

echo GetValueArray('conf', 'test_value'); // echos '1'
echo GetValueArray('conf', 'test_value2'); // echos '2'


?>