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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With