配置类-从函数字符串参数获取配置数组 [英] Configuration class - Get configuration array from the function string argument

查看:56
本文介绍了配置类-从函数字符串参数获取配置数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的功能:

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

我使用此函数从数组接收值.我的问题是我不能像这样使用此功能:

Im using this function to receive a value from an array. My problem is that i cannot use this function like this:

GetValueArray('conf', 'test_value');

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

How could i convert 'conf' to the real array named conf to receive my 'test_value'?

推荐答案

由于函数具有自己的作用域,因此请确保全局化"您要查找的变量.

Because functions have their own scope, be sure to 'globalize' the variable that you're looking into.

但是正如Rizier123所说,您可以在变量周围使用方括号来动态获取/设置变量.

But as Rizier123 said, you can use brackets around a variable to dynamically get/set variables.

<?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'


?>

这篇关于配置类-从函数字符串参数获取配置数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆