array_difference

Returns elements that exist in the first array but not in the second array. Similar to set subtraction (A - B).

{% assign all_skus = "SKU1,SKU2,SKU3,SKU4,SKU5" | split: "," %}
{% assign processed_skus = "SKU2,SKU4" | split: "," %}

{% assign remaining = all_skus | array_difference: processed_skus %}

Output:

SKU1, SKU3, SKU5

Syntax

{{ arrayA | array_difference: arrayB }}
Parameter
Description

arrayA

Source array

arrayB

Array of elements to exclude

Visual Representation

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

Result:  [1, 2]  (elements in A that are NOT in B)

Examples

Find products not yet synced:

Find removed tags:

Output:

Identify discontinued SKUs:

Filter out excluded customers:

Find orders missing from fulfillment report:

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 don't exist in arrayB

  • Returns empty array if all elements of arrayA exist in arrayB

  • Comparison uses strict equality (===)

  • See also: array_intersection, array_symmetric_difference

Last updated