How to remove duplicated items in array on Twig

后端 未结 7 1049
-上瘾入骨i
-上瘾入骨i 2020-12-14 06:40

How to remove duplicated items in array on Twig?

I have array value in twig such as.

{{ set array = [\"testA         


        
7条回答
  •  一向
    一向 (楼主)
    2020-12-14 07:40

    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 %}
    

提交回复
热议问题