Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
$arr[0]=123;
$a="arr[0]";
echo $$a;

gives me error

Notice: Undefined variable: arr[0]

on the last line.

What should I do to make it work?


EDIT:

Above is the simplification of what I want to do. If someone wants to know why I want to do this, then here's the explanation:

This is something like what I want to do:

if(condition){
    $a=$arr1[0][0];
    $b=$arr1[0][1];
    $c=$arr1[0][2];
}
else{
    $a=$arr2[0];
    $b=$arr2[1];
    $c=$arr2[2];
}

I can compact it like this:

if(condition)
    $arr=$arr1[0];
else
    $arr=$arr2;
$a=$arr[0];
$a=$arr[1];
$a=$arr[2];

But I wanted to try doing this using variable variable:

if(condition)
    $arr="$arr1[0]";
else
    $arr="$arr2";
$a={$$arr}[0];
$b={$$arr}[1];
$c={$$arr}[2];

Sure, we don't need variable variables as we can still code without them. I want to know, for learning PHP, why the code won't work.

share|improve this question
1  
You don't need variable variables – Your Common Sense 2 days ago
I need them to compact the code. – user1444680 2 days ago
1  
compact the code ?? you must be joking right ? – Baba 2 days ago
5  
If you want to compact it, use echo 123;. – Gumbo 2 days ago
1  
I guess you mean that it should be $a = $arr[0] ? and then echo $a ? – bestprogrammerintheworld 2 days ago
show 4 more comments

5 Answers

up vote 1 down vote accepted

Now that you said what you’re actually trying to accomplish: Your code doesn’t work because if you look at $arr1[0][0], only arr is the variable name; the [0] are special accessors for certain types like strings or arrays.

With variable variables you can only specify the name but not any accessor or other operation:

A variable variable takes the value of a variable and treats that as the name of a variable.

Your solution with the additional variable holding the array to access later on would be the best solution to your problem.

share|improve this answer
Now that you said what you’re actually trying to accomplish my initial code was enough to convey the problem. – user1444680 2 days ago

What you are trying to do just won't work - the code $arr[0] is referencing a variable called $arr, and then applying the array-access operator ([$key]) to get the element with key 0. There is no variable called $arr[0], so you cannot reference it with variable-variables any more than you could the expression $foo + 1 .

The real question is why you want to do this; variable variables are generally a sign of very messy code, and probably some poor choices of data structure. For instance, if you need to select one of a set of variables based on some input, you probably want a hash, and to look up an item using $hash[$item] or similar. If you need something more complex, a switch statement can often cover the cases you actually need.

If for some reason you really need to allow an arbitrary expression like $arr[0] as input and evaluate it at runtime, you could use eval(), but be very very careful of where the input is coming from, as this can be a very easy way of introducing security holes into your code.

share|improve this answer

As pointed out you don't need variable variables. To get a PHP variable variable name containing index (a key) use array_keys() or array_search() or other array parsers. From php's site:

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array);   // $key = 1;

You could also use the following (using $var= instead of echo):

$arr[0]=123;
$arr[1]=456;
foreach ($arr as $key => $value) {
    echo "arr[{$key}] = {$value} \r\n";
}

Which outputs:

arr[0] = 123
arr[1] = 456

But I don't see why you'd do that, since the whole point of the array is not doing that kind of stuff.

share|improve this answer

FROM PHP DOC

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Use

echo ${$a}[0]; // 123 

Edit : Based on your edit you can simply have

list($a, $b, $c) = (condition) ? $arr1[0] : $arr2;

Or

$array =  (condition) ? $arr1[0] : $arr2;
$a = $array[0];
$b = $array[1];
$c = $array[2];
share|improve this answer
$arr[0]=123;
$a="arr";
$i="0";
echo ${$a}{$i};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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