Skip to the content
Documentation

unique

No ratings yet

Summary

Returns the input array without duplicate elements.

Usage

input|unique()

Returns

The input array containing only one occurance of every element.

Description

This operator removes duplicate elements from the input array.

Examples

Example 1
{array( 1, 2, 2, 3, 4, 4, 5 )|unique}

The following array will be returned: ( 1, 2, 3, 4, 5 ).

Contributors

Comments (1 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. What about objects, nodes ?

    P. Renaud ·

    It doesn't work with nodes.

    Before :
    <ul>
    {foreach $all_related_albums as $related_album}
            <li>{$related_album.main_node.node_id}</li>
    {/foreach}
    </ul>
    
    {def $unique_related_albums=$all_related_albums|unique()}
    
    After :
    <ul>
    {foreach $unique_related_albums as $related_album}
            <li>{$related_album.main_node.node_id}</li>
    {/foreach}
    </ul>

    produces the following output :

    Before :
    7360
    7360
    7360
    7360
    7360
    4166
    
    After :
    7360

    You have to build an intermediate array with node id's, then use unique, then re-fetch all nodes...

Comments

Add a comment

Comments are read by a moderator before they appear.