0

I have set an array in my config file that I use global in my functions.
This works fine, but now I want to pass the name of this array as a @param in my function.

// 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");
flag

80% accept rate
What is your question? – Pekka Mar 24 at 11:09
Can't you just pass your array into your function directly? buildmenu($album_type_arr); – Andy Shellam Mar 24 at 11:11
maybe but I need that name for other stuff, values etc. tnx anyway. – FFish Mar 24 at 11:19

2 Answers

4

You're looking for variable variables:

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

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

You can use "variable variables". This works:

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

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.