12

PHP allow in array to be a space in the key like this:

(dump from symfony2)

array:9 [▼
"Guest" => 1
"Vip Client" => 2
"Super admin" => 3
]

So how can I access the data by key in Twig? Normaly for the Guest it will be {% array_name.Guest %} this will not work {% array_name['Vip Client'] %}

2 Answers 2

12

You can use attribute function

{{ attribute(array_name, 'Vip Client') }}

As suggested here: http://twig.sensiolabs.org/doc/templates.html#variables

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

1 Comment

Thank you It's was very helpfull
8

You can access like this: {{ array_name['Guest'] }} if you want to print. If you want to use it if condition you can write

{% if array_name['Vip Client'] == 1 %} {{'Vip Client'}} {% endif %}

1 Comment

Thank you It's was very helpfull

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.