array_intersection

Returns elements that exist in both arrays. Similar to set intersection (A ∩ B).

{% assign store_a_products = "SKU1,SKU2,SKU3,SKU4" | split: "," %}
{% assign store_b_products = "SKU3,SKU4,SKU5,SKU6" | split: "," %}

{% assign common_products = store_a_products | array_intersection: store_b_products %}

Output:

SKU3, SKU4

Syntax

{{ arrayA | array_intersection: arrayB }}
Parameter
Description

arrayA

First array

arrayB

Second array

Visual Representation

Array A: [1, 2, 3, 4, 5]
Array B: [3, 4, 5, 6, 7]

Result:  [3, 4, 5]  (elements in BOTH A and B)

Examples

Find products in multiple collections:

Match import data with existing catalog:

Find customers who purchased from both categories:

Validate allowed tags:

Find overlapping inventory locations:

Match webhook subscriptions:

Notes

  • Both inputs must be arrays; throws error otherwise

  • Preserves order of elements from the first array

  • Duplicate elements in arrayA are preserved if they also exist in arrayB

  • Returns empty array if no common elements exist

  • Comparison uses strict equality (===)

  • See also: array_difference, array_symmetric_difference

Last updated