Is there any way to get average values from an array when using a Twig template?
arrays, average, php, symfony, twig
Solution
I think this should do it
{% set sumratings = 0 %}
{% for rating in article.ratings %}
{% set sumratings = sumratings + rating.value %}
{% endfor %}
AVG: {{ sumratings / article.ratings|length }}
Assuming `rating.value` is an integer
Problem
Is there any way of getting the average value of an array of value just inside a Twig template when using Symfony2? I tried something like the following code but it doesn't work ``` {% set sumratings = 0 %} {% set count = 0 %} {% for rating in article.ratings %} {% set sumratings = sumratings + rating.value %} {% endfor %} AVG: {{ sumratings / article.ratings.count }} ``` Any idea?