3

Let's say I have an array_1 containing a string which is a name of another array_2. And now I would like to convert the array index into a variable containing the array_2. I have the following working fine:

 $array_1 = array(       
    'ads'    => ['code' => 10000,  'item' => 19999, 'cost' => 'array_2'],
    'offers' => ['code' => 20000,  'item' => 29999, 'cost' => 'array_3'] );

 $array_2 = array(       
    'price'    => '232',
    'surcharge'=> '110'  );

 $ads_prices = $array_1['ads']['cost'];
 $ads_prices = $$ads_prices;
 
 print_r($ads_prices);

 Output:  Array( [price] => 232
                 [surcharge] => 110 )

However, is there a way to make it more succinct by something like this:

 $ads_prices = $array_1['ads'][$$'cost']; 

I could use extract() but it doesn't make the code any more succinct or more efficient.

4
  • @Jimski In your sample code there is no difference between $array_2 and 'Output'. Also there is no $array_3 – what should happen to offers['cost'] in that case? Commented Oct 27 at 19:14
  • Would turning the cost array into more of a lookup table be more dynamic and avoid using variable variables. So each price/surge combination is under another key which would be the equivalent to the 'array_2' and '_3' elements. Commented Oct 27 at 19:19
  • @Nigel Ren I'm not sure if I understand what you mean. By lookup table do you mean include values from array_2 into array_1 ? The array_2 by itself is a lookup table. Commented Oct 27 at 19:34
  • Admittedly, I had a period of fascination and exploration with variable variables. Please allow me to speed you through this phase -- it is always a suboptimal decision. Your IDE / static analyzer will not understand your code. Future human readers of your code will curse your name. Variable variables are effectively arrays, but without access to PHP's robust library of array functions. Whenever you think a script needs a variable variable or eval(), stop and have a re-think. Commented Oct 30 at 11:48

1 Answer 1

4

You can use curly brackets to disambiguate your code. In your case you have:

$ads_prices = $array_1['ads']['cost'];
$ads_prices = $$ads_prices;

So you think you could write:

$ads_prices = $$array_1['ads']['cost'];

But this is ambiguous, and will generate warning/errors and not the result you want. To disambiguate it do:

$ads_prices = ${$array_1['ads']['cost']};

Live demo: https://3v4l.org/E7T5G

I think this is quite poorly documented in the PHP manual, or I missed it. The best example I could find was here: Variable variables. You can also use curly braces for variable property/method names of classes: $myObject->{$myMethod}().

Sign up to request clarification or add additional context in comments.

9 Comments

This does nothing for the key 'offers', where 'cost' is $array_3. In my opinion the question is not covering all cases. Also the output is equal to $array_2, so why query $array_1 at all.
I agree the question has some peculiarities. I restricted myself to answer the question: ".... is there a way to make it more succinct. ...". I think Jimski can take it from there.
I agree, but then the question should be closed, because it would be only about variable variables. And this has been answered several times. My impression is, as stated, that the question is incomplete in one way or another.
Which answer exactly on stackoverflow shows use of dollar sign before curly braces as variable variables.
The array_3 is simply another entry which is accessed in the same manner as array_2, except by using different index, like so: . ${$array_1['offers']['cost']};
You assumed that I know the name of array_2. But if I don't know it (which is the case) then unless I look up the proper index in array_1 I can't access array_2 as I don't know its name.
@lukas.j please point out a good duplicate question. I tried to find one, but couldn't find it. I know it must exist.
$KIKO Software While this is rather esoteric use case, the documentation is indeed rather poor.. I couldn't find proper explanation via search engines.
You can search Stack Overflow by a series of tags. stackoverflow.com/questions/tagged/php+variable-variables

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.