4
{% set key = 'something' %}

My array has key something, I and can it access array['something'], but when I try to use variable in key like:

array[key]

I'm getting error that array key not exist. So question is how to access array keys using variables.

1 Answer 1

7

You can use the attribute function:

{{ attribute(array, key) }}

From the doc:

addition, the defined test can check for the existence of a dynamic attribute:

{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}

Hope this help

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

2 Comments

URL is not working anymore here's the new link to the documentation attribute
Thanks @Thezeus I've updated the link

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.