1

I'd like to use a variable as an array key of unknown depth.

Example:

$a = array();
$arrayKey = '[0][1]';

What I'd like to do is substitute:

$a[0][1] = "two levels deep";

with

${'a'.$arrayKey} = "two levels deep";

but it doesn't seem to work.

echo 'a'.$arrayKey; returns: a[0][1]

echo ${'a'.$arrayKey} returns two levels deep

but I still cannot do

${'a'.$arrayKey} = "two levels deep";
echo $a[0][1];

Is this even possible? It almost seems as though php is parsing a[0][1] as a variable instead of a multidimensional array.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174

2 Answers2

2

Sounds to me like you'll have to use eval, assuming that it is enabled in your php installation.

something like this maybe:

$arrayKey = '[0][1]';
$a = array();
eval("\$a$arrayKey = 'hi there';");
print_r($a);

Array ( [0] => Array ( 1 => hi there )

)

Akom
  • 1,484
  • 19
  • 25
0

Is this even possible?

Yes for one dimensional arrays. No for multidimensional arrays. At least not using variable variables alone. See the duplicate question for alternatives.

From the PHP docs on variable variables:

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.

Multidimensional arrays compound the ambiguity problem. I doubt this is something you can resolve.

Community
  • 1
  • 1
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • I read those docs but that's been accounted for already in my example which adheres somewhat closer to the former syntax `${$a[1]}`. Regardless, what I'm trying here is a bit different ad I'm attempting to declare the index, including brackets with a variable - not reference a already existing index as a variable. – But those new buttons though.. Oct 16 '14 at 19:21
  • Also, if you feel this is a duplicate would you mind offering a link to where? I searched and was unable to find anything which pertains to this. – But those new buttons though.. Oct 16 '14 at 19:22
  • Don't downvote in silence. – Jason McCreary Oct 16 '14 at 19:47
  • Regardless of access or assignment, you'll have the *ambiguity problem*. The link to the duplicate automatically gets posted at the top of your question. – Jason McCreary Oct 16 '14 at 19:47
  • If you need a reason, I down voted your answer because (a) it's not an answer and (b) you suggest that what I'm attempting is not possible which is not true as evidenced by the Akom's answer above. Nothing personal just that you posted a statement which is incorrect. The link to the duplicate however is useful and for that I thank you. – But those new buttons though.. Oct 16 '14 at 19:56
  • a) I did answer it. Nonetheless, I have adjusted it for clarity. b) `eval()` is never an *answer*. – Jason McCreary Oct 16 '14 at 20:16
  • can you explain how it's possible for one dimensional arrays using strictly variable variables? It doesn't appear to be.. – But those new buttons though.. Oct 17 '14 at 06:25