Search code examples
smartysmarty3smarty2

How to remove duplicate values on array using Smarty


I have an array that have duplicated values, I want to print the value only once no matter how many time it exist on the array.

This is the code that I have:

{foreach item="item" from=$root.page.news.item }
{foreach from=$item.Tags item=tagitem key=kex}
    {$tagitem}
{/foreach}
{/foreach}

This is what it prints right:

kim000kardashian
kim000kardashian
miley000cyrus
miley000cyrus
kim000kardashian
irina000shayk

and this is what I am looking to print

kim000kardashian 
miley000cyrus 
irina000shayk

Is there a way to achieve this only using Smarty Templates? or any way that I can use on the .tpl files?

Thanks in advance


Solution

  • {foreach item="item" from=$root.page.news.item }
      {foreach from=$item.Tags item=tagitem key=kex}
        {if !$done.$tagitem}
          {$done.$tagitem = 1}
          {$tagitem}
        {/if}
      {/foreach}
    {/foreach}
    

    I am not sure it works with all the versions.

    Maybe it would be a bit cleaner to call an array_unique() in the php.