0

The title may be a little confusing. This is my problem:

I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:

$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;

That would print: "hello". Now, I've got a problem with this:

$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;

That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?

Thanks in advance.

miku
  • 161,705
  • 45
  • 286
  • 300
fedejp
  • 898
  • 3
  • 12
  • 20
  • You are mixing variable name and array access here. possible duplicate of [use strings to access (potentially large) multidimensional arrays](http://stackoverflow.com/questions/7003559/use-strings-to-access-potentially-large-multidimensional-arrays) – hakre Jun 28 '12 at 00:20

2 Answers2

1

For the moment, I could only think of something like this:

<?php 
    // literal are simple
    $literal = "Hello";
    $vv = "literal";
    echo $$vv . "\n";
    // prints "Hello"


    // for containers it's not so simple anymore
    $container = array("Hello" => "World");
    $vv = "container";

    $reniatnoc = $$vv;
    echo $reniatnoc["Hello"] . "\n";
    // prints "World"
 ?>

The problem here is that (quoting from php: access array value on the fly):

the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.

Would PHP allow the subscript notation anywhere, one could write this more dense as

echo $$vv["Hello"]

Side note: I guess using variable variables isn't that sane to use in production.

Community
  • 1
  • 1
miku
  • 161,705
  • 45
  • 286
  • 300
  • Thanks a lot for the answer. Unfortunately it doesn't work with my current problem (I should have talk about it in my question, but I wanted the question to be more generic). What I have is this: $variableholder = ($id!=FALSE)?'container[\''.$id.'\']':'container'; And I need it that way because if $id is FALSE then container is just and array (if not is an associative array). Don't know if I made it clear enough. – fedejp Jun 28 '12 at 00:27
  • I see. Disclaimer: I'm no PHP pro. But I've never seen [variable variables](http://php.net/manual/language.variables.variable.php) until today (I have used PHP on and off for maybe seven years). My first impression was: wow, this is possible with PHP? As I suspected, this language feature made it into the [list of code smells](http://stackoverflow.com/questions/4273244/auditing-a-php-codebase) - so just as a sidenode, maybe you should not rely on such constructs at all (at least in a production setting). – miku Jun 28 '12 at 00:40
  • Thanks a lot once again. Yeah, I chose to make an IF sentence and repeat the code with the difference that if $id is FALSE the variable has no index; if is not, it has. I just wanted to make it more elegant. Thanks a lot for the advice. – fedejp Jun 28 '12 at 00:52
1

How about this? (NOTE: variable variables are as bad as goto)

$variablename = 'array';
$key = 'index';

echo $$variablename[$key];
Lusitanian
  • 10,789
  • 1
  • 37
  • 38
  • Ah, good point. You'd need to assign $$variablename to a $foobar first. But variable variables are such awful practice anyway that this conversation shouldn't exist :/ – Lusitanian Jun 28 '12 at 00:56