# array\_intersection

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

```liquid
{% 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

```liquid
{{ 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:**

```liquid
{% assign sale_product_ids = sale_collection.products | map: "id" %}
{% assign featured_product_ids = featured_collection.products | map: "id" %}

{% assign featured_sale_items = sale_product_ids | array_intersection: featured_product_ids %}

{% log "Products that are both featured AND on sale: " | append: featured_sale_items.size %}
```

**Match import data with existing catalog:**

```liquid
{% assign existing_skus = shopify_variants | map: "sku" %}
{% assign import_skus = import_data | map: "sku" %}

{% assign matching_skus = existing_skus | array_intersection: import_skus %}
{% assign new_skus = import_skus | array_difference: existing_skus %}

{% log "SKUs to update: " | append: matching_skus.size %}
{% log "New SKUs to create: " | append: new_skus.size %}
```

**Find customers who purchased from both categories:**

```liquid
{% assign electronics_buyers = electronics_orders | map: "customer_id" | uniq %}
{% assign clothing_buyers = clothing_orders | map: "customer_id" | uniq %}

{% assign cross_category_customers = electronics_buyers | array_intersection: clothing_buyers %}

{% log "Customers who bought both electronics and clothing: " | append: cross_category_customers.size %}
```

**Validate allowed tags:**

```liquid
{% assign product_tags = product.tags | split: ", " %}
{% assign allowed_tags = "sale,featured,new,clearance,bestseller" | split: "," %}

{% assign valid_tags = product_tags | array_intersection: allowed_tags %}
{% assign invalid_tags = product_tags | array_difference: allowed_tags %}

{% if invalid_tags.size > 0 %}
  {% log "Warning: Invalid tags found: " | append: invalid_tags | join: ", " %}
{% endif %}
```

**Find overlapping inventory locations:**

```liquid
{% assign product_a_locations = product_a.inventory_levels | map: "location_id" %}
{% assign product_b_locations = product_b.inventory_levels | map: "location_id" %}

{% assign shared_locations = product_a_locations | array_intersection: product_b_locations %}

{% log "Both products available at " | append: shared_locations.size | append: " locations" %}
```

**Match webhook subscriptions:**

```liquid
{% assign required_topics = "orders/create,orders/updated,products/update" | split: "," %}
{% assign registered_topics = existing_webhooks | map: "topic" %}

{% assign covered_topics = required_topics | array_intersection: registered_topics %}
{% assign missing_topics = required_topics | array_difference: registered_topics %}

{% if missing_topics.size > 0 %}
  {% log "Missing webhook subscriptions:" %}
  {% log missing_topics %}
{% else %}
  {% log "All required webhooks are registered" %}
{% endif %}
```

#### 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`
