{% set key = 'something' %}
我的array
有关键内容,我可以访问array['something']
,但当我尝试在密钥中使用变量时:
array[key]
我收到数组密钥不存在的错误。所以问题是如何使用变量访问数组键。
答案 0 :(得分:4)
您可以使用attribute功能:
{{ attribute(array, key) }}
来自doc:
此外,定义的测试可以检查是否存在动态 属性:
{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}
希望这个帮助