Zum Hauptinhalt springen

Unique array

Anatomy

<array:unique var="var" var_result="(destructive on 'var')" key="string"/>

Description: ARRAY:UNIQUE discards duplicate items from an array, keeping only the first occurrence of each value.

A given key may specify the column of a multidimensional array to distinguish by. If however, the key is omitted, then the array is treated as an unidimensional list.

Attention:

If a key is specified and that key does not exist within the inner array, the corresponding item of the outer array will be discarded.

ARRAY:UNIQUE is destructive, in the sense that it modifies the array in-place, unless 'var_result' is specified in which case a copy of the resulting array will be stored in 'var_result' instead. If a value has several occurrences within the array, only the first key will be used while all other items with that particular key will be discarded.

Attributes

NameTypeDescriptionDefined By
varvarVariable name array:unique
var_resultvarResult variable name array:unique

Multidimensional

NameTypeDescriptionDefined By
keystringKey array:unique

Results:

BindingTypePredicate
varvar_resultarray

Examples

Example

<array var="names">
<item key="bg1">Bill Gates</item>
<item key="sj">Steve Jobs</item>
<item key="bg2">Bill Gates</item>
</array>

<array:unique var="names"/>

<foreach var="names" var_key="key" var_value="name">
<output>$key: $name&n;</output>
</foreach>

<!--
bg1: Bill Gates
sj: Steve Jobs
-->