Everybody knows that you can access a variable in PHP using this: ${‘varName’}. But when you need to get/set a variable witch is part of an array, why doesn’t it work ? Suppose we have this piece of code:
<?php
$myArray = array(...);
$myVarName = "myArray['my1']['my11']['my111']";
${$myVarName} = "new value";
?>
Shouldn’t it work ?
I have tested it again and again – it is not working..
Is it there a way to do that?
I recommend you not to use dynamic variables like
${$var}.What you want is modifying a multi-dimensional associative array according to a path of keys.
This is a procedural example to keep it simple. You may want to put your hierarchical array and this functions inside a class.