1

I have a variable which holds an assoc array:

$arr = [
    'key' => [
        'sub' => 'Hello'
    ]
];

I have also another varibale which is a string matching the sub key:

$str = (string) '["key"]["sub"]';

How can I make $arr var to echo the sub key in the $str variable dynamicaly? Maybe in this direction.

echo $arr . ${$str};

Thanks in advance.

user558134
  • 1,071
  • 6
  • 25
  • 38

1 Answers1

1

Don't do this. If you have a specific requirement and you think this is the way then please ask a more general question that does not presuppose this method. But since you're just curious:

eval('echo $arr'.$str.';');
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87