intersect

Description

Returns the intersection of two lists.

 list intersect( list list1 , list list2 )

Return the elements of list1 that are also in list2.

Examples

 {intersect([1, 2, 3, 4], [3, 4, 5, 6])} ---> [3, 4]

Show Feed Items that Match Terms in Custom Interest Array

Use Case: You have users who select writers/authors to follow on your site in case they have new content from them. You’d like to use intersect() to find all the unique authors in the data feed and alert the users with a blurb about any authors who they’re following that have new content.

Zephyr:

On the profile:
zephyr example following stephen king chuck and kurt array

In the Setup:

{allAuthors = []}
{foreach content as c}
{if !contains(allAuthors,c.author)}
{allAuthors = allAuthors + c.author}
{/if}
{/foreach}
{yourAuthors = intersect(profile.vars.following,allAuthors)}

In the Code:

New stories from authors you're following, including:
{foreach yourAuthors as c}
{c}<br/>
{/foreach}

Output:

New stories from authors you’re following, including:
Stephen King
Chuck Palahniuk

Explanation:

This script creates an empty array called “allAuthors”, and it then uses a foreach loop to iterate through the content feed. Within the loops, the contains() function in conjunction with an “if” statement checks to see if the author of the item (c.author) is contained in the with array. If it isn’t, the value for the author variable is added to the array. If it is, then the loop moves on to the next item. Please note that the bang (“!”) acts as a “not” statement, so the “if” statement is checking to see that the array does not contain the author value.

Next, the intersect() function finds the overlap of the “allAuthors” array and the “following” array on the user profile. Whatever the overlap is (i.e., the authors that the user is following are in the feed) gets set as the value of the “yourAuthors” array. Finally, in the Code, the “yourAuthors” array is looped through, displaying to the user which authors they’re following that are in the feed.

Top