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.

4

1 Answer 1

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.';');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.