How to remove duplicated items in array on Twig

Twig is a VIEW engine, and should not be used – in theory – to manipulate data. It’s a (very) good practice to use (assumingly) PHP to gather data, do all necessary manipulations and then pass the right data to your view.

That said, here’s how you can do it in pure Twig syntax:

{% set newArray = [] %}

{% for name in array %}
   {% if name not in newArray %}
     My name is {{name}}
   {% set newArray = newArray|merge([name]) %}
   {% endif %}


{% endfor %}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)