在使用Symfony2时,是否有办法在Twig模板中仅获取数组值的平均值?我尝试了以下代码,但它无效。
{% set sumratings = 0 %}
{% set count = 0 %}
{% for rating in article.ratings %}
{% set sumratings = sumratings + rating.value %}
{% endfor %}
AVG: {{ sumratings / article.ratings.count }}
有任何想法吗?
$avg = count( $sum / $values );
,对吧?你应该这样写$avg = $sum / count( $values );
。 - Peter Bailey