我试图将一个数组从数据库输出到屏幕。
在我的实体中:
/**
* @ORM\Column(type="array", nullable=true)
*/
private $category;
在我的 Twig 模板中:
{% for category in user.profile.category %}
{{ category }}
{% endfor %}
错误:
Array to string conversion in ...
我的错误在哪里?
最佳答案
您可以使用 join
将数组输出为连接字符串。它的行为类似于 implode()在 php.ini 中。
例子:
{{ [1, 2, 3]|join }}
{# returns 123 #}
{{ [1, 2, 3]|join('|') }}
{# outputs 1|2|3 #}
{{ [1, 2, 3]|join(', ', ' and ') }}
{# outputs 1, 2 and 3 #}
见twig join documentation .
关于arrays - Twig 中的输出数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26340115/