gpt4 book ai didi

Twig 使用变量访问数组值

转载 作者:行者123 更新时间:2023-12-02 08:18:25 25 4
gpt4 key购买 nike

{% set key = 'something' %}

我的 array 有关键的东西,我可以访问 array['something'],但是当我尝试在关键中使用变量时:

array[key]

我收到数组键不存在的错误。所以问题是如何使用变量访问数组键。

最佳答案

您可以使用 attribute功能:

{{ attribute(array, key) }}

来自文档:

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

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

希望对你有帮助

关于Twig 使用变量访问数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39829656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com