Skip to main content

Array intersection

Anatomy

<array:intersect var="var" var_set="var" var_result="(destructive on 'var')" type="values"/>

Description: ARRAY:INTERSECT generates the intersection of two arrays by extracting those items of an array that also exist within the set array.

Attention:

ARRAY:INTERSECT 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. It preserves the key and value associations of the array.

Attributes

NameTypeDescriptionDefined By
varvarVariable name array:intersect
var_setvarSet variable name array:intersect
var_resultvarResult variable name array:intersect
typetypeIntersection type array:intersect

Results:

BindingTypePredicate
varvar_resultarray

Examples

Intersection by keys

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

<array var="intersect">
<item key="sj">Steve Jobs</item>
<item key="le">Larry Ellison</item>
<item key="bg">Bill Gates</item>
</array>

<array:intersect var="names" var_set="intersect" type="keys"/>
<output>$names.bg and $names.sj are competitors!</output>

<!-- Bill Gates and Steve Jobs are competitors! -->

Intersection by values

<array var="names">
<item>Bill Gates</item>
<item>Steve Jobs</item>
</array>

<array var="intersect">
<item>Steve Jobs</item>
<item>Larry Ellison</item>
<item>Bill Gates</item>
</array>

<array:intersect var="names" var_set="intersect" type="values"/>
<output>$names[0] and $names[1] are competitors!</output>

<!-- Bill Gates and Steve Jobs are competitors! -->