QGIS – Selecting Features Matching Values in an Array Using QGIS 3.10 Expression

arrayexpressionqgis

I want to use an expression to select features matching the values of an array.

Select features from a list is done by the IN operator:

"field" IN ('value1','value2')

But how to select features from an array like ['value1','value2']? Do I have to convert it to a list (somehow) or is there a "direct" method of doing so?

QGIS 3.10

Best Answer

The expression builder has a whole section of functions for dealing with arrays.

array Returns an array containing all the values passed as parameter

array_append Returns an array with the given value added at the end

array_cat Returns an array containing all the given arrays concatenated

array_contains Returns true if an array contains the given value

array_distinct Returns an array containing distinct values of the given array

array_filter Returns an array with only the items for which an expression evaluates to true

array_find Returns the index (0 for the first one) of a value within an array. Returns -1 if the value is not found.

array_first Returns the first value of an array

array_foreach Returns an array with the given expression evaluated on each item

array_get Returns the Nth value (0 for the first one) of an array

array_insert Returns an array with the given value added at the given position

array_intersect Returns true if any element of array_1 exists in array_2

array_last Returns the last element of an array

array_length Returns the number of elements of an array

array_prepend Returns an array with the given value added at the beginning

array_remove_all Returns an array with all the entries of the given value removed

array_remove_at Returns an array with the given index removed

array_reverse Returns the given array with array values in reversed order

array_slice Returns the values of the array from the start_pos argument up to and including the end_pos argument

array_to_string Concatenates array elements into a string separated by a delimiter and using optional string for empty values

generate_series Creates an array containing a sequence of numbers

regexp_matches Returns an array of all strings captured by capturing groups, in the order the groups themselves appear in the supplied regular expression against a string

string_to_array Splits string into an array using supplied delimiter and optional string for empty values

(Source: QGIS User Manual)

Use array_contains(your_array, "field") to test whether your array contains the current field value.