变量中的PHP变量

时间:2010-03-24 11:07:31

标签: php variables global

我在配置文件中设置了一个数组,我在我的函数中使用了全局数据 这很好用,但现在我想在我的函数中将这个数组的名称作为@param传递。

// in config file:
$album_type_arr = array("appartamento", "villa");   

global $album_type_arr; // pull in from db_config
echo $album_type_arr[0];

function buildmenu($name) {
    $test = global $name . "_arr";
    echo $test[0];
}
buildmenu("album_type");

2 个答案:

答案 0 :(得分:4)

您正在寻找变量:

http://www.php.net/manual/en/language.variables.variable.php

function buildmenu($name) {
    $test = $name . "_arr";
    global ${$test};
    echo ${$test}[0];
}

答案 1 :(得分:0)

您可以使用“variable variables”。这有效:

function buildmenu($name) {
   global ${$name. '_arr'};
   $test = ${$name. '_arr'};
   echo $test[0];
}