You can do this using eval, you donβt know if itβs convenient for you:
$array['some']['string'] = 'test'; $str = '[some][string]'; $code = sprintf('return $array%s;', str_replace(array('[',']'), array('[\'', '\']'), $str)); $value = eval($code); echo $value; # test
However, eval is not always the right tool, because well, it most often shows that you have a design flaw when you need to use it.
Another example, if you need to write access to an array element, you can do the following:
$array['some']['string'] = 'test'; $str = '[some][string]'; $path = explode('][', substr($str, 1, -1)); $value = &$array; foreach($path as $segment) { $value = &$value[$segment]; } echo $value; $value = 'changed'; print_r($array);
This is actually the same principle as Eric's answer , but referring to a variable.
hakre source share