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");
3
  • Can't you just pass your array into your function directly? buildmenu($album_type_arr); Commented Mar 24, 2010 at 11:11
  • maybe but I need that name for other stuff, values etc. tnx anyway. Commented Mar 24, 2010 at 11:19
  • duplicate of Can I use a generated variable name in PHP? Commented Oct 1, 2010 at 4:05

2 Answers 2

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];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use "variable variables". This works:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.