{% set key = 'something' %}
我的 array
有关键的东西,我可以访问 array['something']
,但是当我尝试在关键中使用变量时:
array[key]
我收到数组键不存在的错误。所以问题是如何使用变量访问数组键。
最佳答案
您可以使用 attribute功能:
{{ attribute(array, key) }}
来自文档:
addition, the defined test can check for the existence of a dynamic attribute:
{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}
希望对你有帮助
关于Twig 使用变量访问数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39829656/