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.

3
  • 1
    you're using $arrayKey and $arrayKeyA - is that intentional or just a typo...? Commented Oct 16, 2014 at 17:31
  • 1
    I'd make a function for this. I hacked up something quickly here: eval.in/206790 (I tried to post this as an answer, but the question was closed) Commented Oct 16, 2014 at 18:04
  • Pretty creative solution Rocket Hazmat! Thanks for sharing Commented Oct 17, 2014 at 6:21

2 Answers 2

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 )

)

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

3 Comments

While, eval does work, it's not recommended. There are better ways.
for this particular problem, I really wonder which better way does exists though... :)
Seems a bit messy but does indeed work! Thanks for the idea.. though I'd still like to know if there's a cleaner approach.
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.

7 Comments

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.
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.
Don't downvote in silence.
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.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.